dquot.c 70 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593
  1. /*
  2. * Implementation of the diskquota system for the LINUX operating system. QUOTA
  3. * is implemented using the BSD system call interface as the means of
  4. * communication with the user level. This file contains the generic routines
  5. * called by the different filesystems on allocation of an inode or block.
  6. * These routines take care of the administration needed to have a consistent
  7. * diskquota tracking system. The ideas of both user and group quotas are based
  8. * on the Melbourne quota system as used on BSD derived systems. The internal
  9. * implementation is based on one of the several variants of the LINUX
  10. * inode-subsystem with added complexity of the diskquota system.
  11. *
  12. * Author: Marco van Wieringen <mvw@planets.elm.net>
  13. *
  14. * Fixes: Dmitry Gorodchanin <pgmdsg@ibi.com>, 11 Feb 96
  15. *
  16. * Revised list management to avoid races
  17. * -- Bill Hawes, <whawes@star.net>, 9/98
  18. *
  19. * Fixed races in dquot_transfer(), dqget() and dquot_alloc_...().
  20. * As the consequence the locking was moved from dquot_decr_...(),
  21. * dquot_incr_...() to calling functions.
  22. * invalidate_dquots() now writes modified dquots.
  23. * Serialized quota_off() and quota_on() for mount point.
  24. * Fixed a few bugs in grow_dquots().
  25. * Fixed deadlock in write_dquot() - we no longer account quotas on
  26. * quota files
  27. * remove_dquot_ref() moved to inode.c - it now traverses through inodes
  28. * add_dquot_ref() restarts after blocking
  29. * Added check for bogus uid and fixed check for group in quotactl.
  30. * Jan Kara, <jack@suse.cz>, sponsored by SuSE CR, 10-11/99
  31. *
  32. * Used struct list_head instead of own list struct
  33. * Invalidation of referenced dquots is no longer possible
  34. * Improved free_dquots list management
  35. * Quota and i_blocks are now updated in one place to avoid races
  36. * Warnings are now delayed so we won't block in critical section
  37. * Write updated not to require dquot lock
  38. * Jan Kara, <jack@suse.cz>, 9/2000
  39. *
  40. * Added dynamic quota structure allocation
  41. * Jan Kara <jack@suse.cz> 12/2000
  42. *
  43. * Rewritten quota interface. Implemented new quota format and
  44. * formats registering.
  45. * Jan Kara, <jack@suse.cz>, 2001,2002
  46. *
  47. * New SMP locking.
  48. * Jan Kara, <jack@suse.cz>, 10/2002
  49. *
  50. * Added journalled quota support, fix lock inversion problems
  51. * Jan Kara, <jack@suse.cz>, 2003,2004
  52. *
  53. * (C) Copyright 1994 - 1997 Marco van Wieringen
  54. */
  55. #include <linux/errno.h>
  56. #include <linux/kernel.h>
  57. #include <linux/fs.h>
  58. #include <linux/mount.h>
  59. #include <linux/mm.h>
  60. #include <linux/time.h>
  61. #include <linux/types.h>
  62. #include <linux/string.h>
  63. #include <linux/fcntl.h>
  64. #include <linux/stat.h>
  65. #include <linux/tty.h>
  66. #include <linux/file.h>
  67. #include <linux/slab.h>
  68. #include <linux/sysctl.h>
  69. #include <linux/init.h>
  70. #include <linux/module.h>
  71. #include <linux/proc_fs.h>
  72. #include <linux/security.h>
  73. #include <linux/kmod.h>
  74. #include <linux/namei.h>
  75. #include <linux/buffer_head.h>
  76. #include <linux/capability.h>
  77. #include <linux/quotaops.h>
  78. #include <linux/writeback.h> /* for inode_lock, oddly enough.. */
  79. #include <asm/uaccess.h>
  80. #define __DQUOT_PARANOIA
  81. /*
  82. * There are three quota SMP locks. dq_list_lock protects all lists with quotas
  83. * and quota formats, dqstats structure containing statistics about the lists
  84. * dq_data_lock protects data from dq_dqb and also mem_dqinfo structures and
  85. * also guards consistency of dquot->dq_dqb with inode->i_blocks, i_bytes.
  86. * i_blocks and i_bytes updates itself are guarded by i_lock acquired directly
  87. * in inode_add_bytes() and inode_sub_bytes(). dq_state_lock protects
  88. * modifications of quota state (on quotaon and quotaoff) and readers who care
  89. * about latest values take it as well.
  90. *
  91. * The spinlock ordering is hence: dq_data_lock > dq_list_lock > i_lock,
  92. * dq_list_lock > dq_state_lock
  93. *
  94. * Note that some things (eg. sb pointer, type, id) doesn't change during
  95. * the life of the dquot structure and so needn't to be protected by a lock
  96. *
  97. * Any operation working on dquots via inode pointers must hold dqptr_sem. If
  98. * operation is just reading pointers from inode (or not using them at all) the
  99. * read lock is enough. If pointers are altered function must hold write lock.
  100. * Special care needs to be taken about S_NOQUOTA inode flag (marking that
  101. * inode is a quota file). Functions adding pointers from inode to dquots have
  102. * to check this flag under dqptr_sem and then (if S_NOQUOTA is not set) they
  103. * have to do all pointer modifications before dropping dqptr_sem. This makes
  104. * sure they cannot race with quotaon which first sets S_NOQUOTA flag and
  105. * then drops all pointers to dquots from an inode.
  106. *
  107. * Each dquot has its dq_lock mutex. Locked dquots might not be referenced
  108. * from inodes (dquot_alloc_space() and such don't check the dq_lock).
  109. * Currently dquot is locked only when it is being read to memory (or space for
  110. * it is being allocated) on the first dqget() and when it is being released on
  111. * the last dqput(). The allocation and release oparations are serialized by
  112. * the dq_lock and by checking the use count in dquot_release(). Write
  113. * operations on dquots don't hold dq_lock as they copy data under dq_data_lock
  114. * spinlock to internal buffers before writing.
  115. *
  116. * Lock ordering (including related VFS locks) is the following:
  117. * i_mutex > dqonoff_sem > journal_lock > dqptr_sem > dquot->dq_lock >
  118. * dqio_mutex
  119. * The lock ordering of dqptr_sem imposed by quota code is only dqonoff_sem >
  120. * dqptr_sem. But filesystem has to count with the fact that functions such as
  121. * dquot_alloc_space() acquire dqptr_sem and they usually have to be called
  122. * from inside a transaction to keep filesystem consistency after a crash. Also
  123. * filesystems usually want to do some IO on dquot from ->mark_dirty which is
  124. * called with dqptr_sem held.
  125. * i_mutex on quota files is special (it's below dqio_mutex)
  126. */
  127. static __cacheline_aligned_in_smp DEFINE_SPINLOCK(dq_list_lock);
  128. static __cacheline_aligned_in_smp DEFINE_SPINLOCK(dq_state_lock);
  129. __cacheline_aligned_in_smp DEFINE_SPINLOCK(dq_data_lock);
  130. EXPORT_SYMBOL(dq_data_lock);
  131. static char *quotatypes[] = INITQFNAMES;
  132. static struct quota_format_type *quota_formats; /* List of registered formats */
  133. static struct quota_module_name module_names[] = INIT_QUOTA_MODULE_NAMES;
  134. /* SLAB cache for dquot structures */
  135. static struct kmem_cache *dquot_cachep;
  136. int register_quota_format(struct quota_format_type *fmt)
  137. {
  138. spin_lock(&dq_list_lock);
  139. fmt->qf_next = quota_formats;
  140. quota_formats = fmt;
  141. spin_unlock(&dq_list_lock);
  142. return 0;
  143. }
  144. EXPORT_SYMBOL(register_quota_format);
  145. void unregister_quota_format(struct quota_format_type *fmt)
  146. {
  147. struct quota_format_type **actqf;
  148. spin_lock(&dq_list_lock);
  149. for (actqf = &quota_formats; *actqf && *actqf != fmt;
  150. actqf = &(*actqf)->qf_next)
  151. ;
  152. if (*actqf)
  153. *actqf = (*actqf)->qf_next;
  154. spin_unlock(&dq_list_lock);
  155. }
  156. EXPORT_SYMBOL(unregister_quota_format);
  157. static struct quota_format_type *find_quota_format(int id)
  158. {
  159. struct quota_format_type *actqf;
  160. spin_lock(&dq_list_lock);
  161. for (actqf = quota_formats; actqf && actqf->qf_fmt_id != id;
  162. actqf = actqf->qf_next)
  163. ;
  164. if (!actqf || !try_module_get(actqf->qf_owner)) {
  165. int qm;
  166. spin_unlock(&dq_list_lock);
  167. for (qm = 0; module_names[qm].qm_fmt_id &&
  168. module_names[qm].qm_fmt_id != id; qm++)
  169. ;
  170. if (!module_names[qm].qm_fmt_id ||
  171. request_module(module_names[qm].qm_mod_name))
  172. return NULL;
  173. spin_lock(&dq_list_lock);
  174. for (actqf = quota_formats; actqf && actqf->qf_fmt_id != id;
  175. actqf = actqf->qf_next)
  176. ;
  177. if (actqf && !try_module_get(actqf->qf_owner))
  178. actqf = NULL;
  179. }
  180. spin_unlock(&dq_list_lock);
  181. return actqf;
  182. }
  183. static void put_quota_format(struct quota_format_type *fmt)
  184. {
  185. module_put(fmt->qf_owner);
  186. }
  187. /*
  188. * Dquot List Management:
  189. * The quota code uses three lists for dquot management: the inuse_list,
  190. * free_dquots, and dquot_hash[] array. A single dquot structure may be
  191. * on all three lists, depending on its current state.
  192. *
  193. * All dquots are placed to the end of inuse_list when first created, and this
  194. * list is used for invalidate operation, which must look at every dquot.
  195. *
  196. * Unused dquots (dq_count == 0) are added to the free_dquots list when freed,
  197. * and this list is searched whenever we need an available dquot. Dquots are
  198. * removed from the list as soon as they are used again, and
  199. * dqstats.free_dquots gives the number of dquots on the list. When
  200. * dquot is invalidated it's completely released from memory.
  201. *
  202. * Dquots with a specific identity (device, type and id) are placed on
  203. * one of the dquot_hash[] hash chains. The provides an efficient search
  204. * mechanism to locate a specific dquot.
  205. */
  206. static LIST_HEAD(inuse_list);
  207. static LIST_HEAD(free_dquots);
  208. static unsigned int dq_hash_bits, dq_hash_mask;
  209. static struct hlist_head *dquot_hash;
  210. struct dqstats dqstats;
  211. EXPORT_SYMBOL(dqstats);
  212. static qsize_t inode_get_rsv_space(struct inode *inode);
  213. static inline unsigned int
  214. hashfn(const struct super_block *sb, unsigned int id, int type)
  215. {
  216. unsigned long tmp;
  217. tmp = (((unsigned long)sb>>L1_CACHE_SHIFT) ^ id) * (MAXQUOTAS - type);
  218. return (tmp + (tmp >> dq_hash_bits)) & dq_hash_mask;
  219. }
  220. /*
  221. * Following list functions expect dq_list_lock to be held
  222. */
  223. static inline void insert_dquot_hash(struct dquot *dquot)
  224. {
  225. struct hlist_head *head;
  226. head = dquot_hash + hashfn(dquot->dq_sb, dquot->dq_id, dquot->dq_type);
  227. hlist_add_head(&dquot->dq_hash, head);
  228. }
  229. static inline void remove_dquot_hash(struct dquot *dquot)
  230. {
  231. hlist_del_init(&dquot->dq_hash);
  232. }
  233. static struct dquot *find_dquot(unsigned int hashent, struct super_block *sb,
  234. unsigned int id, int type)
  235. {
  236. struct hlist_node *node;
  237. struct dquot *dquot;
  238. hlist_for_each (node, dquot_hash+hashent) {
  239. dquot = hlist_entry(node, struct dquot, dq_hash);
  240. if (dquot->dq_sb == sb && dquot->dq_id == id &&
  241. dquot->dq_type == type)
  242. return dquot;
  243. }
  244. return NULL;
  245. }
  246. /* Add a dquot to the tail of the free list */
  247. static inline void put_dquot_last(struct dquot *dquot)
  248. {
  249. list_add_tail(&dquot->dq_free, &free_dquots);
  250. dqstats.free_dquots++;
  251. }
  252. static inline void remove_free_dquot(struct dquot *dquot)
  253. {
  254. if (list_empty(&dquot->dq_free))
  255. return;
  256. list_del_init(&dquot->dq_free);
  257. dqstats.free_dquots--;
  258. }
  259. static inline void put_inuse(struct dquot *dquot)
  260. {
  261. /* We add to the back of inuse list so we don't have to restart
  262. * when traversing this list and we block */
  263. list_add_tail(&dquot->dq_inuse, &inuse_list);
  264. dqstats.allocated_dquots++;
  265. }
  266. static inline void remove_inuse(struct dquot *dquot)
  267. {
  268. dqstats.allocated_dquots--;
  269. list_del(&dquot->dq_inuse);
  270. }
  271. /*
  272. * End of list functions needing dq_list_lock
  273. */
  274. static void wait_on_dquot(struct dquot *dquot)
  275. {
  276. mutex_lock(&dquot->dq_lock);
  277. mutex_unlock(&dquot->dq_lock);
  278. }
  279. static inline int dquot_dirty(struct dquot *dquot)
  280. {
  281. return test_bit(DQ_MOD_B, &dquot->dq_flags);
  282. }
  283. static inline int mark_dquot_dirty(struct dquot *dquot)
  284. {
  285. return dquot->dq_sb->dq_op->mark_dirty(dquot);
  286. }
  287. int dquot_mark_dquot_dirty(struct dquot *dquot)
  288. {
  289. spin_lock(&dq_list_lock);
  290. if (!test_and_set_bit(DQ_MOD_B, &dquot->dq_flags))
  291. list_add(&dquot->dq_dirty, &sb_dqopt(dquot->dq_sb)->
  292. info[dquot->dq_type].dqi_dirty_list);
  293. spin_unlock(&dq_list_lock);
  294. return 0;
  295. }
  296. EXPORT_SYMBOL(dquot_mark_dquot_dirty);
  297. /* Dirtify all the dquots - this can block when journalling */
  298. static inline int mark_all_dquot_dirty(struct dquot * const *dquot)
  299. {
  300. int ret, err, cnt;
  301. ret = err = 0;
  302. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  303. if (dquot[cnt])
  304. /* Even in case of error we have to continue */
  305. ret = mark_dquot_dirty(dquot[cnt]);
  306. if (!err)
  307. err = ret;
  308. }
  309. return err;
  310. }
  311. static inline void dqput_all(struct dquot **dquot)
  312. {
  313. unsigned int cnt;
  314. for (cnt = 0; cnt < MAXQUOTAS; cnt++)
  315. dqput(dquot[cnt]);
  316. }
  317. /* This function needs dq_list_lock */
  318. static inline int clear_dquot_dirty(struct dquot *dquot)
  319. {
  320. if (!test_and_clear_bit(DQ_MOD_B, &dquot->dq_flags))
  321. return 0;
  322. list_del_init(&dquot->dq_dirty);
  323. return 1;
  324. }
  325. void mark_info_dirty(struct super_block *sb, int type)
  326. {
  327. set_bit(DQF_INFO_DIRTY_B, &sb_dqopt(sb)->info[type].dqi_flags);
  328. }
  329. EXPORT_SYMBOL(mark_info_dirty);
  330. /*
  331. * Read dquot from disk and alloc space for it
  332. */
  333. int dquot_acquire(struct dquot *dquot)
  334. {
  335. int ret = 0, ret2 = 0;
  336. struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
  337. mutex_lock(&dquot->dq_lock);
  338. mutex_lock(&dqopt->dqio_mutex);
  339. if (!test_bit(DQ_READ_B, &dquot->dq_flags))
  340. ret = dqopt->ops[dquot->dq_type]->read_dqblk(dquot);
  341. if (ret < 0)
  342. goto out_iolock;
  343. set_bit(DQ_READ_B, &dquot->dq_flags);
  344. /* Instantiate dquot if needed */
  345. if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags) && !dquot->dq_off) {
  346. ret = dqopt->ops[dquot->dq_type]->commit_dqblk(dquot);
  347. /* Write the info if needed */
  348. if (info_dirty(&dqopt->info[dquot->dq_type])) {
  349. ret2 = dqopt->ops[dquot->dq_type]->write_file_info(
  350. dquot->dq_sb, dquot->dq_type);
  351. }
  352. if (ret < 0)
  353. goto out_iolock;
  354. if (ret2 < 0) {
  355. ret = ret2;
  356. goto out_iolock;
  357. }
  358. }
  359. set_bit(DQ_ACTIVE_B, &dquot->dq_flags);
  360. out_iolock:
  361. mutex_unlock(&dqopt->dqio_mutex);
  362. mutex_unlock(&dquot->dq_lock);
  363. return ret;
  364. }
  365. EXPORT_SYMBOL(dquot_acquire);
  366. /*
  367. * Write dquot to disk
  368. */
  369. int dquot_commit(struct dquot *dquot)
  370. {
  371. int ret = 0, ret2 = 0;
  372. struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
  373. mutex_lock(&dqopt->dqio_mutex);
  374. spin_lock(&dq_list_lock);
  375. if (!clear_dquot_dirty(dquot)) {
  376. spin_unlock(&dq_list_lock);
  377. goto out_sem;
  378. }
  379. spin_unlock(&dq_list_lock);
  380. /* Inactive dquot can be only if there was error during read/init
  381. * => we have better not writing it */
  382. if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
  383. ret = dqopt->ops[dquot->dq_type]->commit_dqblk(dquot);
  384. if (info_dirty(&dqopt->info[dquot->dq_type])) {
  385. ret2 = dqopt->ops[dquot->dq_type]->write_file_info(
  386. dquot->dq_sb, dquot->dq_type);
  387. }
  388. if (ret >= 0)
  389. ret = ret2;
  390. }
  391. out_sem:
  392. mutex_unlock(&dqopt->dqio_mutex);
  393. return ret;
  394. }
  395. EXPORT_SYMBOL(dquot_commit);
  396. /*
  397. * Release dquot
  398. */
  399. int dquot_release(struct dquot *dquot)
  400. {
  401. int ret = 0, ret2 = 0;
  402. struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
  403. mutex_lock(&dquot->dq_lock);
  404. /* Check whether we are not racing with some other dqget() */
  405. if (atomic_read(&dquot->dq_count) > 1)
  406. goto out_dqlock;
  407. mutex_lock(&dqopt->dqio_mutex);
  408. if (dqopt->ops[dquot->dq_type]->release_dqblk) {
  409. ret = dqopt->ops[dquot->dq_type]->release_dqblk(dquot);
  410. /* Write the info */
  411. if (info_dirty(&dqopt->info[dquot->dq_type])) {
  412. ret2 = dqopt->ops[dquot->dq_type]->write_file_info(
  413. dquot->dq_sb, dquot->dq_type);
  414. }
  415. if (ret >= 0)
  416. ret = ret2;
  417. }
  418. clear_bit(DQ_ACTIVE_B, &dquot->dq_flags);
  419. mutex_unlock(&dqopt->dqio_mutex);
  420. out_dqlock:
  421. mutex_unlock(&dquot->dq_lock);
  422. return ret;
  423. }
  424. EXPORT_SYMBOL(dquot_release);
  425. void dquot_destroy(struct dquot *dquot)
  426. {
  427. kmem_cache_free(dquot_cachep, dquot);
  428. }
  429. EXPORT_SYMBOL(dquot_destroy);
  430. static inline void do_destroy_dquot(struct dquot *dquot)
  431. {
  432. dquot->dq_sb->dq_op->destroy_dquot(dquot);
  433. }
  434. /* Invalidate all dquots on the list. Note that this function is called after
  435. * quota is disabled and pointers from inodes removed so there cannot be new
  436. * quota users. There can still be some users of quotas due to inodes being
  437. * just deleted or pruned by prune_icache() (those are not attached to any
  438. * list) or parallel quotactl call. We have to wait for such users.
  439. */
  440. static void invalidate_dquots(struct super_block *sb, int type)
  441. {
  442. struct dquot *dquot, *tmp;
  443. restart:
  444. spin_lock(&dq_list_lock);
  445. list_for_each_entry_safe(dquot, tmp, &inuse_list, dq_inuse) {
  446. if (dquot->dq_sb != sb)
  447. continue;
  448. if (dquot->dq_type != type)
  449. continue;
  450. /* Wait for dquot users */
  451. if (atomic_read(&dquot->dq_count)) {
  452. DEFINE_WAIT(wait);
  453. atomic_inc(&dquot->dq_count);
  454. prepare_to_wait(&dquot->dq_wait_unused, &wait,
  455. TASK_UNINTERRUPTIBLE);
  456. spin_unlock(&dq_list_lock);
  457. /* Once dqput() wakes us up, we know it's time to free
  458. * the dquot.
  459. * IMPORTANT: we rely on the fact that there is always
  460. * at most one process waiting for dquot to free.
  461. * Otherwise dq_count would be > 1 and we would never
  462. * wake up.
  463. */
  464. if (atomic_read(&dquot->dq_count) > 1)
  465. schedule();
  466. finish_wait(&dquot->dq_wait_unused, &wait);
  467. dqput(dquot);
  468. /* At this moment dquot() need not exist (it could be
  469. * reclaimed by prune_dqcache(). Hence we must
  470. * restart. */
  471. goto restart;
  472. }
  473. /*
  474. * Quota now has no users and it has been written on last
  475. * dqput()
  476. */
  477. remove_dquot_hash(dquot);
  478. remove_free_dquot(dquot);
  479. remove_inuse(dquot);
  480. do_destroy_dquot(dquot);
  481. }
  482. spin_unlock(&dq_list_lock);
  483. }
  484. /* Call callback for every active dquot on given filesystem */
  485. int dquot_scan_active(struct super_block *sb,
  486. int (*fn)(struct dquot *dquot, unsigned long priv),
  487. unsigned long priv)
  488. {
  489. struct dquot *dquot, *old_dquot = NULL;
  490. int ret = 0;
  491. mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
  492. spin_lock(&dq_list_lock);
  493. list_for_each_entry(dquot, &inuse_list, dq_inuse) {
  494. if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags))
  495. continue;
  496. if (dquot->dq_sb != sb)
  497. continue;
  498. /* Now we have active dquot so we can just increase use count */
  499. atomic_inc(&dquot->dq_count);
  500. dqstats.lookups++;
  501. spin_unlock(&dq_list_lock);
  502. dqput(old_dquot);
  503. old_dquot = dquot;
  504. ret = fn(dquot, priv);
  505. if (ret < 0)
  506. goto out;
  507. spin_lock(&dq_list_lock);
  508. /* We are safe to continue now because our dquot could not
  509. * be moved out of the inuse list while we hold the reference */
  510. }
  511. spin_unlock(&dq_list_lock);
  512. out:
  513. dqput(old_dquot);
  514. mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
  515. return ret;
  516. }
  517. EXPORT_SYMBOL(dquot_scan_active);
  518. int vfs_quota_sync(struct super_block *sb, int type, int wait)
  519. {
  520. struct list_head *dirty;
  521. struct dquot *dquot;
  522. struct quota_info *dqopt = sb_dqopt(sb);
  523. int cnt;
  524. mutex_lock(&dqopt->dqonoff_mutex);
  525. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  526. if (type != -1 && cnt != type)
  527. continue;
  528. if (!sb_has_quota_active(sb, cnt))
  529. continue;
  530. spin_lock(&dq_list_lock);
  531. dirty = &dqopt->info[cnt].dqi_dirty_list;
  532. while (!list_empty(dirty)) {
  533. dquot = list_first_entry(dirty, struct dquot,
  534. dq_dirty);
  535. /* Dirty and inactive can be only bad dquot... */
  536. if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
  537. clear_dquot_dirty(dquot);
  538. continue;
  539. }
  540. /* Now we have active dquot from which someone is
  541. * holding reference so we can safely just increase
  542. * use count */
  543. atomic_inc(&dquot->dq_count);
  544. dqstats.lookups++;
  545. spin_unlock(&dq_list_lock);
  546. sb->dq_op->write_dquot(dquot);
  547. dqput(dquot);
  548. spin_lock(&dq_list_lock);
  549. }
  550. spin_unlock(&dq_list_lock);
  551. }
  552. for (cnt = 0; cnt < MAXQUOTAS; cnt++)
  553. if ((cnt == type || type == -1) && sb_has_quota_active(sb, cnt)
  554. && info_dirty(&dqopt->info[cnt]))
  555. sb->dq_op->write_info(sb, cnt);
  556. spin_lock(&dq_list_lock);
  557. dqstats.syncs++;
  558. spin_unlock(&dq_list_lock);
  559. mutex_unlock(&dqopt->dqonoff_mutex);
  560. if (!wait || (sb_dqopt(sb)->flags & DQUOT_QUOTA_SYS_FILE))
  561. return 0;
  562. /* This is not very clever (and fast) but currently I don't know about
  563. * any other simple way of getting quota data to disk and we must get
  564. * them there for userspace to be visible... */
  565. if (sb->s_op->sync_fs)
  566. sb->s_op->sync_fs(sb, 1);
  567. sync_blockdev(sb->s_bdev);
  568. /*
  569. * Now when everything is written we can discard the pagecache so
  570. * that userspace sees the changes.
  571. */
  572. mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
  573. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  574. if (type != -1 && cnt != type)
  575. continue;
  576. if (!sb_has_quota_active(sb, cnt))
  577. continue;
  578. mutex_lock_nested(&sb_dqopt(sb)->files[cnt]->i_mutex,
  579. I_MUTEX_QUOTA);
  580. truncate_inode_pages(&sb_dqopt(sb)->files[cnt]->i_data, 0);
  581. mutex_unlock(&sb_dqopt(sb)->files[cnt]->i_mutex);
  582. }
  583. mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
  584. return 0;
  585. }
  586. EXPORT_SYMBOL(vfs_quota_sync);
  587. /* Free unused dquots from cache */
  588. static void prune_dqcache(int count)
  589. {
  590. struct list_head *head;
  591. struct dquot *dquot;
  592. head = free_dquots.prev;
  593. while (head != &free_dquots && count) {
  594. dquot = list_entry(head, struct dquot, dq_free);
  595. remove_dquot_hash(dquot);
  596. remove_free_dquot(dquot);
  597. remove_inuse(dquot);
  598. do_destroy_dquot(dquot);
  599. count--;
  600. head = free_dquots.prev;
  601. }
  602. }
  603. /*
  604. * This is called from kswapd when we think we need some
  605. * more memory
  606. */
  607. static int shrink_dqcache_memory(int nr, gfp_t gfp_mask)
  608. {
  609. if (nr) {
  610. spin_lock(&dq_list_lock);
  611. prune_dqcache(nr);
  612. spin_unlock(&dq_list_lock);
  613. }
  614. return (dqstats.free_dquots / 100) * sysctl_vfs_cache_pressure;
  615. }
  616. static struct shrinker dqcache_shrinker = {
  617. .shrink = shrink_dqcache_memory,
  618. .seeks = DEFAULT_SEEKS,
  619. };
  620. /*
  621. * Put reference to dquot
  622. * NOTE: If you change this function please check whether dqput_blocks() works right...
  623. */
  624. void dqput(struct dquot *dquot)
  625. {
  626. int ret;
  627. if (!dquot)
  628. return;
  629. #ifdef __DQUOT_PARANOIA
  630. if (!atomic_read(&dquot->dq_count)) {
  631. printk("VFS: dqput: trying to free free dquot\n");
  632. printk("VFS: device %s, dquot of %s %d\n",
  633. dquot->dq_sb->s_id,
  634. quotatypes[dquot->dq_type],
  635. dquot->dq_id);
  636. BUG();
  637. }
  638. #endif
  639. spin_lock(&dq_list_lock);
  640. dqstats.drops++;
  641. spin_unlock(&dq_list_lock);
  642. we_slept:
  643. spin_lock(&dq_list_lock);
  644. if (atomic_read(&dquot->dq_count) > 1) {
  645. /* We have more than one user... nothing to do */
  646. atomic_dec(&dquot->dq_count);
  647. /* Releasing dquot during quotaoff phase? */
  648. if (!sb_has_quota_active(dquot->dq_sb, dquot->dq_type) &&
  649. atomic_read(&dquot->dq_count) == 1)
  650. wake_up(&dquot->dq_wait_unused);
  651. spin_unlock(&dq_list_lock);
  652. return;
  653. }
  654. /* Need to release dquot? */
  655. if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags) && dquot_dirty(dquot)) {
  656. spin_unlock(&dq_list_lock);
  657. /* Commit dquot before releasing */
  658. ret = dquot->dq_sb->dq_op->write_dquot(dquot);
  659. if (ret < 0) {
  660. printk(KERN_ERR "VFS: cannot write quota structure on "
  661. "device %s (error %d). Quota may get out of "
  662. "sync!\n", dquot->dq_sb->s_id, ret);
  663. /*
  664. * We clear dirty bit anyway, so that we avoid
  665. * infinite loop here
  666. */
  667. spin_lock(&dq_list_lock);
  668. clear_dquot_dirty(dquot);
  669. spin_unlock(&dq_list_lock);
  670. }
  671. goto we_slept;
  672. }
  673. /* Clear flag in case dquot was inactive (something bad happened) */
  674. clear_dquot_dirty(dquot);
  675. if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
  676. spin_unlock(&dq_list_lock);
  677. dquot->dq_sb->dq_op->release_dquot(dquot);
  678. goto we_slept;
  679. }
  680. atomic_dec(&dquot->dq_count);
  681. #ifdef __DQUOT_PARANOIA
  682. /* sanity check */
  683. BUG_ON(!list_empty(&dquot->dq_free));
  684. #endif
  685. put_dquot_last(dquot);
  686. spin_unlock(&dq_list_lock);
  687. }
  688. EXPORT_SYMBOL(dqput);
  689. struct dquot *dquot_alloc(struct super_block *sb, int type)
  690. {
  691. return kmem_cache_zalloc(dquot_cachep, GFP_NOFS);
  692. }
  693. EXPORT_SYMBOL(dquot_alloc);
  694. static struct dquot *get_empty_dquot(struct super_block *sb, int type)
  695. {
  696. struct dquot *dquot;
  697. dquot = sb->dq_op->alloc_dquot(sb, type);
  698. if(!dquot)
  699. return NULL;
  700. mutex_init(&dquot->dq_lock);
  701. INIT_LIST_HEAD(&dquot->dq_free);
  702. INIT_LIST_HEAD(&dquot->dq_inuse);
  703. INIT_HLIST_NODE(&dquot->dq_hash);
  704. INIT_LIST_HEAD(&dquot->dq_dirty);
  705. init_waitqueue_head(&dquot->dq_wait_unused);
  706. dquot->dq_sb = sb;
  707. dquot->dq_type = type;
  708. atomic_set(&dquot->dq_count, 1);
  709. return dquot;
  710. }
  711. /*
  712. * Get reference to dquot
  713. *
  714. * Locking is slightly tricky here. We are guarded from parallel quotaoff()
  715. * destroying our dquot by:
  716. * a) checking for quota flags under dq_list_lock and
  717. * b) getting a reference to dquot before we release dq_list_lock
  718. */
  719. struct dquot *dqget(struct super_block *sb, unsigned int id, int type)
  720. {
  721. unsigned int hashent = hashfn(sb, id, type);
  722. struct dquot *dquot = NULL, *empty = NULL;
  723. if (!sb_has_quota_active(sb, type))
  724. return NULL;
  725. we_slept:
  726. spin_lock(&dq_list_lock);
  727. spin_lock(&dq_state_lock);
  728. if (!sb_has_quota_active(sb, type)) {
  729. spin_unlock(&dq_state_lock);
  730. spin_unlock(&dq_list_lock);
  731. goto out;
  732. }
  733. spin_unlock(&dq_state_lock);
  734. dquot = find_dquot(hashent, sb, id, type);
  735. if (!dquot) {
  736. if (!empty) {
  737. spin_unlock(&dq_list_lock);
  738. empty = get_empty_dquot(sb, type);
  739. if (!empty)
  740. schedule(); /* Try to wait for a moment... */
  741. goto we_slept;
  742. }
  743. dquot = empty;
  744. empty = NULL;
  745. dquot->dq_id = id;
  746. /* all dquots go on the inuse_list */
  747. put_inuse(dquot);
  748. /* hash it first so it can be found */
  749. insert_dquot_hash(dquot);
  750. dqstats.lookups++;
  751. spin_unlock(&dq_list_lock);
  752. } else {
  753. if (!atomic_read(&dquot->dq_count))
  754. remove_free_dquot(dquot);
  755. atomic_inc(&dquot->dq_count);
  756. dqstats.cache_hits++;
  757. dqstats.lookups++;
  758. spin_unlock(&dq_list_lock);
  759. }
  760. /* Wait for dq_lock - after this we know that either dquot_release() is
  761. * already finished or it will be canceled due to dq_count > 1 test */
  762. wait_on_dquot(dquot);
  763. /* Read the dquot / allocate space in quota file */
  764. if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags) &&
  765. sb->dq_op->acquire_dquot(dquot) < 0) {
  766. dqput(dquot);
  767. dquot = NULL;
  768. goto out;
  769. }
  770. #ifdef __DQUOT_PARANOIA
  771. BUG_ON(!dquot->dq_sb); /* Has somebody invalidated entry under us? */
  772. #endif
  773. out:
  774. if (empty)
  775. do_destroy_dquot(empty);
  776. return dquot;
  777. }
  778. EXPORT_SYMBOL(dqget);
  779. static int dqinit_needed(struct inode *inode, int type)
  780. {
  781. int cnt;
  782. if (IS_NOQUOTA(inode))
  783. return 0;
  784. if (type != -1)
  785. return !inode->i_dquot[type];
  786. for (cnt = 0; cnt < MAXQUOTAS; cnt++)
  787. if (!inode->i_dquot[cnt])
  788. return 1;
  789. return 0;
  790. }
  791. /* This routine is guarded by dqonoff_mutex mutex */
  792. static void add_dquot_ref(struct super_block *sb, int type)
  793. {
  794. struct inode *inode, *old_inode = NULL;
  795. int reserved = 0;
  796. spin_lock(&inode_lock);
  797. list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
  798. if (inode->i_state & (I_FREEING|I_CLEAR|I_WILL_FREE|I_NEW))
  799. continue;
  800. if (unlikely(inode_get_rsv_space(inode) > 0))
  801. reserved = 1;
  802. if (!atomic_read(&inode->i_writecount))
  803. continue;
  804. if (!dqinit_needed(inode, type))
  805. continue;
  806. __iget(inode);
  807. spin_unlock(&inode_lock);
  808. iput(old_inode);
  809. sb->dq_op->initialize(inode, type);
  810. /* We hold a reference to 'inode' so it couldn't have been
  811. * removed from s_inodes list while we dropped the inode_lock.
  812. * We cannot iput the inode now as we can be holding the last
  813. * reference and we cannot iput it under inode_lock. So we
  814. * keep the reference and iput it later. */
  815. old_inode = inode;
  816. spin_lock(&inode_lock);
  817. }
  818. spin_unlock(&inode_lock);
  819. iput(old_inode);
  820. if (reserved) {
  821. printk(KERN_WARNING "VFS (%s): Writes happened before quota"
  822. " was turned on thus quota information is probably "
  823. "inconsistent. Please run quotacheck(8).\n", sb->s_id);
  824. }
  825. }
  826. /*
  827. * Return 0 if dqput() won't block.
  828. * (note that 1 doesn't necessarily mean blocking)
  829. */
  830. static inline int dqput_blocks(struct dquot *dquot)
  831. {
  832. if (atomic_read(&dquot->dq_count) <= 1)
  833. return 1;
  834. return 0;
  835. }
  836. /*
  837. * Remove references to dquots from inode and add dquot to list for freeing
  838. * if we have the last referece to dquot
  839. * We can't race with anybody because we hold dqptr_sem for writing...
  840. */
  841. static int remove_inode_dquot_ref(struct inode *inode, int type,
  842. struct list_head *tofree_head)
  843. {
  844. struct dquot *dquot = inode->i_dquot[type];
  845. inode->i_dquot[type] = NULL;
  846. if (dquot) {
  847. if (dqput_blocks(dquot)) {
  848. #ifdef __DQUOT_PARANOIA
  849. if (atomic_read(&dquot->dq_count) != 1)
  850. printk(KERN_WARNING "VFS: Adding dquot with dq_count %d to dispose list.\n", atomic_read(&dquot->dq_count));
  851. #endif
  852. spin_lock(&dq_list_lock);
  853. /* As dquot must have currently users it can't be on
  854. * the free list... */
  855. list_add(&dquot->dq_free, tofree_head);
  856. spin_unlock(&dq_list_lock);
  857. return 1;
  858. }
  859. else
  860. dqput(dquot); /* We have guaranteed we won't block */
  861. }
  862. return 0;
  863. }
  864. /*
  865. * Free list of dquots
  866. * Dquots are removed from inodes and no new references can be got so we are
  867. * the only ones holding reference
  868. */
  869. static void put_dquot_list(struct list_head *tofree_head)
  870. {
  871. struct list_head *act_head;
  872. struct dquot *dquot;
  873. act_head = tofree_head->next;
  874. while (act_head != tofree_head) {
  875. dquot = list_entry(act_head, struct dquot, dq_free);
  876. act_head = act_head->next;
  877. /* Remove dquot from the list so we won't have problems... */
  878. list_del_init(&dquot->dq_free);
  879. dqput(dquot);
  880. }
  881. }
  882. static void remove_dquot_ref(struct super_block *sb, int type,
  883. struct list_head *tofree_head)
  884. {
  885. struct inode *inode;
  886. spin_lock(&inode_lock);
  887. list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
  888. /*
  889. * We have to scan also I_NEW inodes because they can already
  890. * have quota pointer initialized. Luckily, we need to touch
  891. * only quota pointers and these have separate locking
  892. * (dqptr_sem).
  893. */
  894. if (!IS_NOQUOTA(inode))
  895. remove_inode_dquot_ref(inode, type, tofree_head);
  896. }
  897. spin_unlock(&inode_lock);
  898. }
  899. /* Gather all references from inodes and drop them */
  900. static void drop_dquot_ref(struct super_block *sb, int type)
  901. {
  902. LIST_HEAD(tofree_head);
  903. if (sb->dq_op) {
  904. down_write(&sb_dqopt(sb)->dqptr_sem);
  905. remove_dquot_ref(sb, type, &tofree_head);
  906. up_write(&sb_dqopt(sb)->dqptr_sem);
  907. put_dquot_list(&tofree_head);
  908. }
  909. }
  910. static inline void dquot_incr_inodes(struct dquot *dquot, qsize_t number)
  911. {
  912. dquot->dq_dqb.dqb_curinodes += number;
  913. }
  914. static inline void dquot_incr_space(struct dquot *dquot, qsize_t number)
  915. {
  916. dquot->dq_dqb.dqb_curspace += number;
  917. }
  918. static inline void dquot_resv_space(struct dquot *dquot, qsize_t number)
  919. {
  920. dquot->dq_dqb.dqb_rsvspace += number;
  921. }
  922. /*
  923. * Claim reserved quota space
  924. */
  925. static void dquot_claim_reserved_space(struct dquot *dquot, qsize_t number)
  926. {
  927. if (dquot->dq_dqb.dqb_rsvspace < number) {
  928. WARN_ON_ONCE(1);
  929. number = dquot->dq_dqb.dqb_rsvspace;
  930. }
  931. dquot->dq_dqb.dqb_curspace += number;
  932. dquot->dq_dqb.dqb_rsvspace -= number;
  933. }
  934. static inline
  935. void dquot_free_reserved_space(struct dquot *dquot, qsize_t number)
  936. {
  937. if (dquot->dq_dqb.dqb_rsvspace >= number)
  938. dquot->dq_dqb.dqb_rsvspace -= number;
  939. else {
  940. WARN_ON_ONCE(1);
  941. dquot->dq_dqb.dqb_rsvspace = 0;
  942. }
  943. }
  944. static void dquot_decr_inodes(struct dquot *dquot, qsize_t number)
  945. {
  946. if (sb_dqopt(dquot->dq_sb)->flags & DQUOT_NEGATIVE_USAGE ||
  947. dquot->dq_dqb.dqb_curinodes >= number)
  948. dquot->dq_dqb.dqb_curinodes -= number;
  949. else
  950. dquot->dq_dqb.dqb_curinodes = 0;
  951. if (dquot->dq_dqb.dqb_curinodes <= dquot->dq_dqb.dqb_isoftlimit)
  952. dquot->dq_dqb.dqb_itime = (time_t) 0;
  953. clear_bit(DQ_INODES_B, &dquot->dq_flags);
  954. }
  955. static void dquot_decr_space(struct dquot *dquot, qsize_t number)
  956. {
  957. if (sb_dqopt(dquot->dq_sb)->flags & DQUOT_NEGATIVE_USAGE ||
  958. dquot->dq_dqb.dqb_curspace >= number)
  959. dquot->dq_dqb.dqb_curspace -= number;
  960. else
  961. dquot->dq_dqb.dqb_curspace = 0;
  962. if (dquot->dq_dqb.dqb_curspace <= dquot->dq_dqb.dqb_bsoftlimit)
  963. dquot->dq_dqb.dqb_btime = (time_t) 0;
  964. clear_bit(DQ_BLKS_B, &dquot->dq_flags);
  965. }
  966. static int warning_issued(struct dquot *dquot, const int warntype)
  967. {
  968. int flag = (warntype == QUOTA_NL_BHARDWARN ||
  969. warntype == QUOTA_NL_BSOFTLONGWARN) ? DQ_BLKS_B :
  970. ((warntype == QUOTA_NL_IHARDWARN ||
  971. warntype == QUOTA_NL_ISOFTLONGWARN) ? DQ_INODES_B : 0);
  972. if (!flag)
  973. return 0;
  974. return test_and_set_bit(flag, &dquot->dq_flags);
  975. }
  976. #ifdef CONFIG_PRINT_QUOTA_WARNING
  977. static int flag_print_warnings = 1;
  978. static int need_print_warning(struct dquot *dquot)
  979. {
  980. if (!flag_print_warnings)
  981. return 0;
  982. switch (dquot->dq_type) {
  983. case USRQUOTA:
  984. return current_fsuid() == dquot->dq_id;
  985. case GRPQUOTA:
  986. return in_group_p(dquot->dq_id);
  987. }
  988. return 0;
  989. }
  990. /* Print warning to user which exceeded quota */
  991. static void print_warning(struct dquot *dquot, const int warntype)
  992. {
  993. char *msg = NULL;
  994. struct tty_struct *tty;
  995. if (warntype == QUOTA_NL_IHARDBELOW ||
  996. warntype == QUOTA_NL_ISOFTBELOW ||
  997. warntype == QUOTA_NL_BHARDBELOW ||
  998. warntype == QUOTA_NL_BSOFTBELOW || !need_print_warning(dquot))
  999. return;
  1000. tty = get_current_tty();
  1001. if (!tty)
  1002. return;
  1003. tty_write_message(tty, dquot->dq_sb->s_id);
  1004. if (warntype == QUOTA_NL_ISOFTWARN || warntype == QUOTA_NL_BSOFTWARN)
  1005. tty_write_message(tty, ": warning, ");
  1006. else
  1007. tty_write_message(tty, ": write failed, ");
  1008. tty_write_message(tty, quotatypes[dquot->dq_type]);
  1009. switch (warntype) {
  1010. case QUOTA_NL_IHARDWARN:
  1011. msg = " file limit reached.\r\n";
  1012. break;
  1013. case QUOTA_NL_ISOFTLONGWARN:
  1014. msg = " file quota exceeded too long.\r\n";
  1015. break;
  1016. case QUOTA_NL_ISOFTWARN:
  1017. msg = " file quota exceeded.\r\n";
  1018. break;
  1019. case QUOTA_NL_BHARDWARN:
  1020. msg = " block limit reached.\r\n";
  1021. break;
  1022. case QUOTA_NL_BSOFTLONGWARN:
  1023. msg = " block quota exceeded too long.\r\n";
  1024. break;
  1025. case QUOTA_NL_BSOFTWARN:
  1026. msg = " block quota exceeded.\r\n";
  1027. break;
  1028. }
  1029. tty_write_message(tty, msg);
  1030. tty_kref_put(tty);
  1031. }
  1032. #endif
  1033. /*
  1034. * Write warnings to the console and send warning messages over netlink.
  1035. *
  1036. * Note that this function can sleep.
  1037. */
  1038. static void flush_warnings(struct dquot *const *dquots, char *warntype)
  1039. {
  1040. struct dquot *dq;
  1041. int i;
  1042. for (i = 0; i < MAXQUOTAS; i++) {
  1043. dq = dquots[i];
  1044. if (dq && warntype[i] != QUOTA_NL_NOWARN &&
  1045. !warning_issued(dq, warntype[i])) {
  1046. #ifdef CONFIG_PRINT_QUOTA_WARNING
  1047. print_warning(dq, warntype[i]);
  1048. #endif
  1049. quota_send_warning(dq->dq_type, dq->dq_id,
  1050. dq->dq_sb->s_dev, warntype[i]);
  1051. }
  1052. }
  1053. }
  1054. static int ignore_hardlimit(struct dquot *dquot)
  1055. {
  1056. struct mem_dqinfo *info = &sb_dqopt(dquot->dq_sb)->info[dquot->dq_type];
  1057. return capable(CAP_SYS_RESOURCE) &&
  1058. (info->dqi_format->qf_fmt_id != QFMT_VFS_OLD ||
  1059. !(info->dqi_flags & V1_DQF_RSQUASH));
  1060. }
  1061. /* needs dq_data_lock */
  1062. static int check_idq(struct dquot *dquot, qsize_t inodes, char *warntype)
  1063. {
  1064. qsize_t newinodes = dquot->dq_dqb.dqb_curinodes + inodes;
  1065. *warntype = QUOTA_NL_NOWARN;
  1066. if (!sb_has_quota_limits_enabled(dquot->dq_sb, dquot->dq_type) ||
  1067. test_bit(DQ_FAKE_B, &dquot->dq_flags))
  1068. return QUOTA_OK;
  1069. if (dquot->dq_dqb.dqb_ihardlimit &&
  1070. newinodes > dquot->dq_dqb.dqb_ihardlimit &&
  1071. !ignore_hardlimit(dquot)) {
  1072. *warntype = QUOTA_NL_IHARDWARN;
  1073. return NO_QUOTA;
  1074. }
  1075. if (dquot->dq_dqb.dqb_isoftlimit &&
  1076. newinodes > dquot->dq_dqb.dqb_isoftlimit &&
  1077. dquot->dq_dqb.dqb_itime &&
  1078. get_seconds() >= dquot->dq_dqb.dqb_itime &&
  1079. !ignore_hardlimit(dquot)) {
  1080. *warntype = QUOTA_NL_ISOFTLONGWARN;
  1081. return NO_QUOTA;
  1082. }
  1083. if (dquot->dq_dqb.dqb_isoftlimit &&
  1084. newinodes > dquot->dq_dqb.dqb_isoftlimit &&
  1085. dquot->dq_dqb.dqb_itime == 0) {
  1086. *warntype = QUOTA_NL_ISOFTWARN;
  1087. dquot->dq_dqb.dqb_itime = get_seconds() +
  1088. sb_dqopt(dquot->dq_sb)->info[dquot->dq_type].dqi_igrace;
  1089. }
  1090. return QUOTA_OK;
  1091. }
  1092. /* needs dq_data_lock */
  1093. static int check_bdq(struct dquot *dquot, qsize_t space, int prealloc, char *warntype)
  1094. {
  1095. qsize_t tspace;
  1096. struct super_block *sb = dquot->dq_sb;
  1097. *warntype = QUOTA_NL_NOWARN;
  1098. if (!sb_has_quota_limits_enabled(sb, dquot->dq_type) ||
  1099. test_bit(DQ_FAKE_B, &dquot->dq_flags))
  1100. return QUOTA_OK;
  1101. tspace = dquot->dq_dqb.dqb_curspace + dquot->dq_dqb.dqb_rsvspace
  1102. + space;
  1103. if (dquot->dq_dqb.dqb_bhardlimit &&
  1104. tspace > dquot->dq_dqb.dqb_bhardlimit &&
  1105. !ignore_hardlimit(dquot)) {
  1106. if (!prealloc)
  1107. *warntype = QUOTA_NL_BHARDWARN;
  1108. return NO_QUOTA;
  1109. }
  1110. if (dquot->dq_dqb.dqb_bsoftlimit &&
  1111. tspace > dquot->dq_dqb.dqb_bsoftlimit &&
  1112. dquot->dq_dqb.dqb_btime &&
  1113. get_seconds() >= dquot->dq_dqb.dqb_btime &&
  1114. !ignore_hardlimit(dquot)) {
  1115. if (!prealloc)
  1116. *warntype = QUOTA_NL_BSOFTLONGWARN;
  1117. return NO_QUOTA;
  1118. }
  1119. if (dquot->dq_dqb.dqb_bsoftlimit &&
  1120. tspace > dquot->dq_dqb.dqb_bsoftlimit &&
  1121. dquot->dq_dqb.dqb_btime == 0) {
  1122. if (!prealloc) {
  1123. *warntype = QUOTA_NL_BSOFTWARN;
  1124. dquot->dq_dqb.dqb_btime = get_seconds() +
  1125. sb_dqopt(sb)->info[dquot->dq_type].dqi_bgrace;
  1126. }
  1127. else
  1128. /*
  1129. * We don't allow preallocation to exceed softlimit so exceeding will
  1130. * be always printed
  1131. */
  1132. return NO_QUOTA;
  1133. }
  1134. return QUOTA_OK;
  1135. }
  1136. static int info_idq_free(struct dquot *dquot, qsize_t inodes)
  1137. {
  1138. qsize_t newinodes;
  1139. if (test_bit(DQ_FAKE_B, &dquot->dq_flags) ||
  1140. dquot->dq_dqb.dqb_curinodes <= dquot->dq_dqb.dqb_isoftlimit ||
  1141. !sb_has_quota_limits_enabled(dquot->dq_sb, dquot->dq_type))
  1142. return QUOTA_NL_NOWARN;
  1143. newinodes = dquot->dq_dqb.dqb_curinodes - inodes;
  1144. if (newinodes <= dquot->dq_dqb.dqb_isoftlimit)
  1145. return QUOTA_NL_ISOFTBELOW;
  1146. if (dquot->dq_dqb.dqb_curinodes >= dquot->dq_dqb.dqb_ihardlimit &&
  1147. newinodes < dquot->dq_dqb.dqb_ihardlimit)
  1148. return QUOTA_NL_IHARDBELOW;
  1149. return QUOTA_NL_NOWARN;
  1150. }
  1151. static int info_bdq_free(struct dquot *dquot, qsize_t space)
  1152. {
  1153. if (test_bit(DQ_FAKE_B, &dquot->dq_flags) ||
  1154. dquot->dq_dqb.dqb_curspace <= dquot->dq_dqb.dqb_bsoftlimit)
  1155. return QUOTA_NL_NOWARN;
  1156. if (dquot->dq_dqb.dqb_curspace - space <= dquot->dq_dqb.dqb_bsoftlimit)
  1157. return QUOTA_NL_BSOFTBELOW;
  1158. if (dquot->dq_dqb.dqb_curspace >= dquot->dq_dqb.dqb_bhardlimit &&
  1159. dquot->dq_dqb.dqb_curspace - space < dquot->dq_dqb.dqb_bhardlimit)
  1160. return QUOTA_NL_BHARDBELOW;
  1161. return QUOTA_NL_NOWARN;
  1162. }
  1163. /*
  1164. * Initialize quota pointers in inode
  1165. * We do things in a bit complicated way but by that we avoid calling
  1166. * dqget() and thus filesystem callbacks under dqptr_sem.
  1167. */
  1168. int dquot_initialize(struct inode *inode, int type)
  1169. {
  1170. unsigned int id = 0;
  1171. int cnt, ret = 0;
  1172. struct dquot *got[MAXQUOTAS];
  1173. struct super_block *sb = inode->i_sb;
  1174. qsize_t rsv;
  1175. /* First test before acquiring mutex - solves deadlocks when we
  1176. * re-enter the quota code and are already holding the mutex */
  1177. if (IS_NOQUOTA(inode))
  1178. return 0;
  1179. /* First get references to structures we might need. */
  1180. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1181. got[cnt] = NULL;
  1182. if (type != -1 && cnt != type)
  1183. continue;
  1184. switch (cnt) {
  1185. case USRQUOTA:
  1186. id = inode->i_uid;
  1187. break;
  1188. case GRPQUOTA:
  1189. id = inode->i_gid;
  1190. break;
  1191. }
  1192. got[cnt] = dqget(sb, id, cnt);
  1193. }
  1194. down_write(&sb_dqopt(sb)->dqptr_sem);
  1195. if (IS_NOQUOTA(inode))
  1196. goto out_err;
  1197. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1198. if (type != -1 && cnt != type)
  1199. continue;
  1200. /* Avoid races with quotaoff() */
  1201. if (!sb_has_quota_active(sb, cnt))
  1202. continue;
  1203. if (!inode->i_dquot[cnt]) {
  1204. inode->i_dquot[cnt] = got[cnt];
  1205. got[cnt] = NULL;
  1206. /*
  1207. * Make quota reservation system happy if someone
  1208. * did a write before quota was turned on
  1209. */
  1210. rsv = inode_get_rsv_space(inode);
  1211. if (unlikely(rsv))
  1212. dquot_resv_space(inode->i_dquot[cnt], rsv);
  1213. }
  1214. }
  1215. out_err:
  1216. up_write(&sb_dqopt(sb)->dqptr_sem);
  1217. /* Drop unused references */
  1218. dqput_all(got);
  1219. return ret;
  1220. }
  1221. EXPORT_SYMBOL(dquot_initialize);
  1222. /*
  1223. * Release all quotas referenced by inode
  1224. */
  1225. int dquot_drop(struct inode *inode)
  1226. {
  1227. int cnt;
  1228. struct dquot *put[MAXQUOTAS];
  1229. down_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1230. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1231. put[cnt] = inode->i_dquot[cnt];
  1232. inode->i_dquot[cnt] = NULL;
  1233. }
  1234. up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1235. dqput_all(put);
  1236. return 0;
  1237. }
  1238. EXPORT_SYMBOL(dquot_drop);
  1239. /* Wrapper to remove references to quota structures from inode */
  1240. void vfs_dq_drop(struct inode *inode)
  1241. {
  1242. /* Here we can get arbitrary inode from clear_inode() so we have
  1243. * to be careful. OTOH we don't need locking as quota operations
  1244. * are allowed to change only at mount time */
  1245. if (!IS_NOQUOTA(inode) && inode->i_sb && inode->i_sb->dq_op
  1246. && inode->i_sb->dq_op->drop) {
  1247. int cnt;
  1248. /* Test before calling to rule out calls from proc and such
  1249. * where we are not allowed to block. Note that this is
  1250. * actually reliable test even without the lock - the caller
  1251. * must assure that nobody can come after the DQUOT_DROP and
  1252. * add quota pointers back anyway */
  1253. for (cnt = 0; cnt < MAXQUOTAS; cnt++)
  1254. if (inode->i_dquot[cnt])
  1255. break;
  1256. if (cnt < MAXQUOTAS)
  1257. inode->i_sb->dq_op->drop(inode);
  1258. }
  1259. }
  1260. EXPORT_SYMBOL(vfs_dq_drop);
  1261. /*
  1262. * inode_reserved_space is managed internally by quota, and protected by
  1263. * i_lock similar to i_blocks+i_bytes.
  1264. */
  1265. static qsize_t *inode_reserved_space(struct inode * inode)
  1266. {
  1267. /* Filesystem must explicitly define it's own method in order to use
  1268. * quota reservation interface */
  1269. BUG_ON(!inode->i_sb->dq_op->get_reserved_space);
  1270. return inode->i_sb->dq_op->get_reserved_space(inode);
  1271. }
  1272. void inode_add_rsv_space(struct inode *inode, qsize_t number)
  1273. {
  1274. spin_lock(&inode->i_lock);
  1275. *inode_reserved_space(inode) += number;
  1276. spin_unlock(&inode->i_lock);
  1277. }
  1278. EXPORT_SYMBOL(inode_add_rsv_space);
  1279. void inode_claim_rsv_space(struct inode *inode, qsize_t number)
  1280. {
  1281. spin_lock(&inode->i_lock);
  1282. *inode_reserved_space(inode) -= number;
  1283. __inode_add_bytes(inode, number);
  1284. spin_unlock(&inode->i_lock);
  1285. }
  1286. EXPORT_SYMBOL(inode_claim_rsv_space);
  1287. void inode_sub_rsv_space(struct inode *inode, qsize_t number)
  1288. {
  1289. spin_lock(&inode->i_lock);
  1290. *inode_reserved_space(inode) -= number;
  1291. spin_unlock(&inode->i_lock);
  1292. }
  1293. EXPORT_SYMBOL(inode_sub_rsv_space);
  1294. static qsize_t inode_get_rsv_space(struct inode *inode)
  1295. {
  1296. qsize_t ret;
  1297. if (!inode->i_sb->dq_op->get_reserved_space)
  1298. return 0;
  1299. spin_lock(&inode->i_lock);
  1300. ret = *inode_reserved_space(inode);
  1301. spin_unlock(&inode->i_lock);
  1302. return ret;
  1303. }
  1304. static void inode_incr_space(struct inode *inode, qsize_t number,
  1305. int reserve)
  1306. {
  1307. if (reserve)
  1308. inode_add_rsv_space(inode, number);
  1309. else
  1310. inode_add_bytes(inode, number);
  1311. }
  1312. static void inode_decr_space(struct inode *inode, qsize_t number, int reserve)
  1313. {
  1314. if (reserve)
  1315. inode_sub_rsv_space(inode, number);
  1316. else
  1317. inode_sub_bytes(inode, number);
  1318. }
  1319. /*
  1320. * Following four functions update i_blocks+i_bytes fields and
  1321. * quota information (together with appropriate checks)
  1322. * NOTE: We absolutely rely on the fact that caller dirties
  1323. * the inode (usually macros in quotaops.h care about this) and
  1324. * holds a handle for the current transaction so that dquot write and
  1325. * inode write go into the same transaction.
  1326. */
  1327. /*
  1328. * This operation can block, but only after everything is updated
  1329. */
  1330. int __dquot_alloc_space(struct inode *inode, qsize_t number,
  1331. int warn, int reserve)
  1332. {
  1333. int cnt, ret = QUOTA_OK;
  1334. char warntype[MAXQUOTAS];
  1335. /*
  1336. * First test before acquiring mutex - solves deadlocks when we
  1337. * re-enter the quota code and are already holding the mutex
  1338. */
  1339. if (IS_NOQUOTA(inode)) {
  1340. inode_incr_space(inode, number, reserve);
  1341. goto out;
  1342. }
  1343. down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1344. for (cnt = 0; cnt < MAXQUOTAS; cnt++)
  1345. warntype[cnt] = QUOTA_NL_NOWARN;
  1346. spin_lock(&dq_data_lock);
  1347. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1348. if (!inode->i_dquot[cnt])
  1349. continue;
  1350. if (check_bdq(inode->i_dquot[cnt], number, warn, warntype+cnt)
  1351. == NO_QUOTA) {
  1352. ret = NO_QUOTA;
  1353. spin_unlock(&dq_data_lock);
  1354. goto out_flush_warn;
  1355. }
  1356. }
  1357. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1358. if (!inode->i_dquot[cnt])
  1359. continue;
  1360. if (reserve)
  1361. dquot_resv_space(inode->i_dquot[cnt], number);
  1362. else
  1363. dquot_incr_space(inode->i_dquot[cnt], number);
  1364. }
  1365. inode_incr_space(inode, number, reserve);
  1366. spin_unlock(&dq_data_lock);
  1367. if (reserve)
  1368. goto out_flush_warn;
  1369. mark_all_dquot_dirty(inode->i_dquot);
  1370. out_flush_warn:
  1371. flush_warnings(inode->i_dquot, warntype);
  1372. up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1373. out:
  1374. return ret;
  1375. }
  1376. int dquot_alloc_space(struct inode *inode, qsize_t number, int warn)
  1377. {
  1378. return __dquot_alloc_space(inode, number, warn, 0);
  1379. }
  1380. EXPORT_SYMBOL(dquot_alloc_space);
  1381. int dquot_reserve_space(struct inode *inode, qsize_t number, int warn)
  1382. {
  1383. return __dquot_alloc_space(inode, number, warn, 1);
  1384. }
  1385. EXPORT_SYMBOL(dquot_reserve_space);
  1386. /*
  1387. * This operation can block, but only after everything is updated
  1388. */
  1389. int dquot_alloc_inode(const struct inode *inode, qsize_t number)
  1390. {
  1391. int cnt, ret = NO_QUOTA;
  1392. char warntype[MAXQUOTAS];
  1393. /* First test before acquiring mutex - solves deadlocks when we
  1394. * re-enter the quota code and are already holding the mutex */
  1395. if (IS_NOQUOTA(inode))
  1396. return QUOTA_OK;
  1397. for (cnt = 0; cnt < MAXQUOTAS; cnt++)
  1398. warntype[cnt] = QUOTA_NL_NOWARN;
  1399. down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1400. spin_lock(&dq_data_lock);
  1401. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1402. if (!inode->i_dquot[cnt])
  1403. continue;
  1404. if (check_idq(inode->i_dquot[cnt], number, warntype+cnt)
  1405. == NO_QUOTA)
  1406. goto warn_put_all;
  1407. }
  1408. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1409. if (!inode->i_dquot[cnt])
  1410. continue;
  1411. dquot_incr_inodes(inode->i_dquot[cnt], number);
  1412. }
  1413. ret = QUOTA_OK;
  1414. warn_put_all:
  1415. spin_unlock(&dq_data_lock);
  1416. if (ret == QUOTA_OK)
  1417. mark_all_dquot_dirty(inode->i_dquot);
  1418. flush_warnings(inode->i_dquot, warntype);
  1419. up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1420. return ret;
  1421. }
  1422. EXPORT_SYMBOL(dquot_alloc_inode);
  1423. int dquot_claim_space(struct inode *inode, qsize_t number)
  1424. {
  1425. int cnt;
  1426. int ret = QUOTA_OK;
  1427. if (IS_NOQUOTA(inode)) {
  1428. inode_claim_rsv_space(inode, number);
  1429. goto out;
  1430. }
  1431. down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1432. spin_lock(&dq_data_lock);
  1433. /* Claim reserved quotas to allocated quotas */
  1434. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1435. if (inode->i_dquot[cnt])
  1436. dquot_claim_reserved_space(inode->i_dquot[cnt],
  1437. number);
  1438. }
  1439. /* Update inode bytes */
  1440. inode_claim_rsv_space(inode, number);
  1441. spin_unlock(&dq_data_lock);
  1442. mark_all_dquot_dirty(inode->i_dquot);
  1443. up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1444. out:
  1445. return ret;
  1446. }
  1447. EXPORT_SYMBOL(dquot_claim_space);
  1448. /*
  1449. * This operation can block, but only after everything is updated
  1450. */
  1451. int __dquot_free_space(struct inode *inode, qsize_t number, int reserve)
  1452. {
  1453. unsigned int cnt;
  1454. char warntype[MAXQUOTAS];
  1455. /* First test before acquiring mutex - solves deadlocks when we
  1456. * re-enter the quota code and are already holding the mutex */
  1457. if (IS_NOQUOTA(inode)) {
  1458. inode_decr_space(inode, number, reserve);
  1459. return QUOTA_OK;
  1460. }
  1461. down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1462. spin_lock(&dq_data_lock);
  1463. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1464. if (!inode->i_dquot[cnt])
  1465. continue;
  1466. warntype[cnt] = info_bdq_free(inode->i_dquot[cnt], number);
  1467. if (reserve)
  1468. dquot_free_reserved_space(inode->i_dquot[cnt], number);
  1469. else
  1470. dquot_decr_space(inode->i_dquot[cnt], number);
  1471. }
  1472. inode_decr_space(inode, number, reserve);
  1473. spin_unlock(&dq_data_lock);
  1474. if (reserve)
  1475. goto out_unlock;
  1476. mark_all_dquot_dirty(inode->i_dquot);
  1477. out_unlock:
  1478. flush_warnings(inode->i_dquot, warntype);
  1479. up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1480. return QUOTA_OK;
  1481. }
  1482. int dquot_free_space(struct inode *inode, qsize_t number)
  1483. {
  1484. return __dquot_free_space(inode, number, 0);
  1485. }
  1486. EXPORT_SYMBOL(dquot_free_space);
  1487. /*
  1488. * Release reserved quota space
  1489. */
  1490. void dquot_release_reserved_space(struct inode *inode, qsize_t number)
  1491. {
  1492. __dquot_free_space(inode, number, 1);
  1493. }
  1494. EXPORT_SYMBOL(dquot_release_reserved_space);
  1495. /*
  1496. * This operation can block, but only after everything is updated
  1497. */
  1498. int dquot_free_inode(const struct inode *inode, qsize_t number)
  1499. {
  1500. unsigned int cnt;
  1501. char warntype[MAXQUOTAS];
  1502. /* First test before acquiring mutex - solves deadlocks when we
  1503. * re-enter the quota code and are already holding the mutex */
  1504. if (IS_NOQUOTA(inode))
  1505. return QUOTA_OK;
  1506. down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1507. spin_lock(&dq_data_lock);
  1508. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1509. if (!inode->i_dquot[cnt])
  1510. continue;
  1511. warntype[cnt] = info_idq_free(inode->i_dquot[cnt], number);
  1512. dquot_decr_inodes(inode->i_dquot[cnt], number);
  1513. }
  1514. spin_unlock(&dq_data_lock);
  1515. mark_all_dquot_dirty(inode->i_dquot);
  1516. flush_warnings(inode->i_dquot, warntype);
  1517. up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1518. return QUOTA_OK;
  1519. }
  1520. EXPORT_SYMBOL(dquot_free_inode);
  1521. /*
  1522. * Transfer the number of inode and blocks from one diskquota to an other.
  1523. *
  1524. * This operation can block, but only after everything is updated
  1525. * A transaction must be started when entering this function.
  1526. */
  1527. int dquot_transfer(struct inode *inode, struct iattr *iattr)
  1528. {
  1529. qsize_t space, cur_space;
  1530. qsize_t rsv_space = 0;
  1531. struct dquot *transfer_from[MAXQUOTAS];
  1532. struct dquot *transfer_to[MAXQUOTAS];
  1533. int cnt, ret = QUOTA_OK;
  1534. int chuid = iattr->ia_valid & ATTR_UID && inode->i_uid != iattr->ia_uid,
  1535. chgid = iattr->ia_valid & ATTR_GID && inode->i_gid != iattr->ia_gid;
  1536. char warntype_to[MAXQUOTAS];
  1537. char warntype_from_inodes[MAXQUOTAS], warntype_from_space[MAXQUOTAS];
  1538. /* First test before acquiring mutex - solves deadlocks when we
  1539. * re-enter the quota code and are already holding the mutex */
  1540. if (IS_NOQUOTA(inode))
  1541. return QUOTA_OK;
  1542. /* Initialize the arrays */
  1543. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1544. transfer_from[cnt] = NULL;
  1545. transfer_to[cnt] = NULL;
  1546. warntype_to[cnt] = QUOTA_NL_NOWARN;
  1547. }
  1548. if (chuid)
  1549. transfer_to[USRQUOTA] = dqget(inode->i_sb, iattr->ia_uid,
  1550. USRQUOTA);
  1551. if (chgid)
  1552. transfer_to[GRPQUOTA] = dqget(inode->i_sb, iattr->ia_gid,
  1553. GRPQUOTA);
  1554. down_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1555. if (IS_NOQUOTA(inode)) { /* File without quota accounting? */
  1556. up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1557. goto put_all;
  1558. }
  1559. spin_lock(&dq_data_lock);
  1560. cur_space = inode_get_bytes(inode);
  1561. rsv_space = inode_get_rsv_space(inode);
  1562. space = cur_space + rsv_space;
  1563. /* Build the transfer_from list and check the limits */
  1564. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1565. if (!transfer_to[cnt])
  1566. continue;
  1567. transfer_from[cnt] = inode->i_dquot[cnt];
  1568. if (check_idq(transfer_to[cnt], 1, warntype_to + cnt) ==
  1569. NO_QUOTA || check_bdq(transfer_to[cnt], space, 0,
  1570. warntype_to + cnt) == NO_QUOTA)
  1571. goto over_quota;
  1572. }
  1573. /*
  1574. * Finally perform the needed transfer from transfer_from to transfer_to
  1575. */
  1576. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1577. /*
  1578. * Skip changes for same uid or gid or for turned off quota-type.
  1579. */
  1580. if (!transfer_to[cnt])
  1581. continue;
  1582. /* Due to IO error we might not have transfer_from[] structure */
  1583. if (transfer_from[cnt]) {
  1584. warntype_from_inodes[cnt] =
  1585. info_idq_free(transfer_from[cnt], 1);
  1586. warntype_from_space[cnt] =
  1587. info_bdq_free(transfer_from[cnt], space);
  1588. dquot_decr_inodes(transfer_from[cnt], 1);
  1589. dquot_decr_space(transfer_from[cnt], cur_space);
  1590. dquot_free_reserved_space(transfer_from[cnt],
  1591. rsv_space);
  1592. }
  1593. dquot_incr_inodes(transfer_to[cnt], 1);
  1594. dquot_incr_space(transfer_to[cnt], cur_space);
  1595. dquot_resv_space(transfer_to[cnt], rsv_space);
  1596. inode->i_dquot[cnt] = transfer_to[cnt];
  1597. }
  1598. spin_unlock(&dq_data_lock);
  1599. up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1600. mark_all_dquot_dirty(transfer_from);
  1601. mark_all_dquot_dirty(transfer_to);
  1602. /* The reference we got is transferred to the inode */
  1603. for (cnt = 0; cnt < MAXQUOTAS; cnt++)
  1604. transfer_to[cnt] = NULL;
  1605. warn_put_all:
  1606. flush_warnings(transfer_to, warntype_to);
  1607. flush_warnings(transfer_from, warntype_from_inodes);
  1608. flush_warnings(transfer_from, warntype_from_space);
  1609. put_all:
  1610. dqput_all(transfer_from);
  1611. dqput_all(transfer_to);
  1612. return ret;
  1613. over_quota:
  1614. spin_unlock(&dq_data_lock);
  1615. up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1616. /* Clear dquot pointers we don't want to dqput() */
  1617. for (cnt = 0; cnt < MAXQUOTAS; cnt++)
  1618. transfer_from[cnt] = NULL;
  1619. ret = NO_QUOTA;
  1620. goto warn_put_all;
  1621. }
  1622. EXPORT_SYMBOL(dquot_transfer);
  1623. /* Wrapper for transferring ownership of an inode */
  1624. int vfs_dq_transfer(struct inode *inode, struct iattr *iattr)
  1625. {
  1626. if (sb_any_quota_active(inode->i_sb) && !IS_NOQUOTA(inode)) {
  1627. vfs_dq_init(inode);
  1628. if (inode->i_sb->dq_op->transfer(inode, iattr) == NO_QUOTA)
  1629. return 1;
  1630. }
  1631. return 0;
  1632. }
  1633. EXPORT_SYMBOL(vfs_dq_transfer);
  1634. /*
  1635. * Write info of quota file to disk
  1636. */
  1637. int dquot_commit_info(struct super_block *sb, int type)
  1638. {
  1639. int ret;
  1640. struct quota_info *dqopt = sb_dqopt(sb);
  1641. mutex_lock(&dqopt->dqio_mutex);
  1642. ret = dqopt->ops[type]->write_file_info(sb, type);
  1643. mutex_unlock(&dqopt->dqio_mutex);
  1644. return ret;
  1645. }
  1646. EXPORT_SYMBOL(dquot_commit_info);
  1647. /*
  1648. * Definitions of diskquota operations.
  1649. */
  1650. const struct dquot_operations dquot_operations = {
  1651. .initialize = dquot_initialize,
  1652. .drop = dquot_drop,
  1653. .alloc_space = dquot_alloc_space,
  1654. .alloc_inode = dquot_alloc_inode,
  1655. .free_space = dquot_free_space,
  1656. .free_inode = dquot_free_inode,
  1657. .transfer = dquot_transfer,
  1658. .write_dquot = dquot_commit,
  1659. .acquire_dquot = dquot_acquire,
  1660. .release_dquot = dquot_release,
  1661. .mark_dirty = dquot_mark_dquot_dirty,
  1662. .write_info = dquot_commit_info,
  1663. .alloc_dquot = dquot_alloc,
  1664. .destroy_dquot = dquot_destroy,
  1665. };
  1666. /*
  1667. * Turn quota off on a device. type == -1 ==> quotaoff for all types (umount)
  1668. */
  1669. int vfs_quota_disable(struct super_block *sb, int type, unsigned int flags)
  1670. {
  1671. int cnt, ret = 0;
  1672. struct quota_info *dqopt = sb_dqopt(sb);
  1673. struct inode *toputinode[MAXQUOTAS];
  1674. /* Cannot turn off usage accounting without turning off limits, or
  1675. * suspend quotas and simultaneously turn quotas off. */
  1676. if ((flags & DQUOT_USAGE_ENABLED && !(flags & DQUOT_LIMITS_ENABLED))
  1677. || (flags & DQUOT_SUSPENDED && flags & (DQUOT_LIMITS_ENABLED |
  1678. DQUOT_USAGE_ENABLED)))
  1679. return -EINVAL;
  1680. /* We need to serialize quota_off() for device */
  1681. mutex_lock(&dqopt->dqonoff_mutex);
  1682. /*
  1683. * Skip everything if there's nothing to do. We have to do this because
  1684. * sometimes we are called when fill_super() failed and calling
  1685. * sync_fs() in such cases does no good.
  1686. */
  1687. if (!sb_any_quota_loaded(sb)) {
  1688. mutex_unlock(&dqopt->dqonoff_mutex);
  1689. return 0;
  1690. }
  1691. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1692. toputinode[cnt] = NULL;
  1693. if (type != -1 && cnt != type)
  1694. continue;
  1695. if (!sb_has_quota_loaded(sb, cnt))
  1696. continue;
  1697. if (flags & DQUOT_SUSPENDED) {
  1698. spin_lock(&dq_state_lock);
  1699. dqopt->flags |=
  1700. dquot_state_flag(DQUOT_SUSPENDED, cnt);
  1701. spin_unlock(&dq_state_lock);
  1702. } else {
  1703. spin_lock(&dq_state_lock);
  1704. dqopt->flags &= ~dquot_state_flag(flags, cnt);
  1705. /* Turning off suspended quotas? */
  1706. if (!sb_has_quota_loaded(sb, cnt) &&
  1707. sb_has_quota_suspended(sb, cnt)) {
  1708. dqopt->flags &= ~dquot_state_flag(
  1709. DQUOT_SUSPENDED, cnt);
  1710. spin_unlock(&dq_state_lock);
  1711. iput(dqopt->files[cnt]);
  1712. dqopt->files[cnt] = NULL;
  1713. continue;
  1714. }
  1715. spin_unlock(&dq_state_lock);
  1716. }
  1717. /* We still have to keep quota loaded? */
  1718. if (sb_has_quota_loaded(sb, cnt) && !(flags & DQUOT_SUSPENDED))
  1719. continue;
  1720. /* Note: these are blocking operations */
  1721. drop_dquot_ref(sb, cnt);
  1722. invalidate_dquots(sb, cnt);
  1723. /*
  1724. * Now all dquots should be invalidated, all writes done so we
  1725. * should be only users of the info. No locks needed.
  1726. */
  1727. if (info_dirty(&dqopt->info[cnt]))
  1728. sb->dq_op->write_info(sb, cnt);
  1729. if (dqopt->ops[cnt]->free_file_info)
  1730. dqopt->ops[cnt]->free_file_info(sb, cnt);
  1731. put_quota_format(dqopt->info[cnt].dqi_format);
  1732. toputinode[cnt] = dqopt->files[cnt];
  1733. if (!sb_has_quota_loaded(sb, cnt))
  1734. dqopt->files[cnt] = NULL;
  1735. dqopt->info[cnt].dqi_flags = 0;
  1736. dqopt->info[cnt].dqi_igrace = 0;
  1737. dqopt->info[cnt].dqi_bgrace = 0;
  1738. dqopt->ops[cnt] = NULL;
  1739. }
  1740. mutex_unlock(&dqopt->dqonoff_mutex);
  1741. /* Skip syncing and setting flags if quota files are hidden */
  1742. if (dqopt->flags & DQUOT_QUOTA_SYS_FILE)
  1743. goto put_inodes;
  1744. /* Sync the superblock so that buffers with quota data are written to
  1745. * disk (and so userspace sees correct data afterwards). */
  1746. if (sb->s_op->sync_fs)
  1747. sb->s_op->sync_fs(sb, 1);
  1748. sync_blockdev(sb->s_bdev);
  1749. /* Now the quota files are just ordinary files and we can set the
  1750. * inode flags back. Moreover we discard the pagecache so that
  1751. * userspace sees the writes we did bypassing the pagecache. We
  1752. * must also discard the blockdev buffers so that we see the
  1753. * changes done by userspace on the next quotaon() */
  1754. for (cnt = 0; cnt < MAXQUOTAS; cnt++)
  1755. if (toputinode[cnt]) {
  1756. mutex_lock(&dqopt->dqonoff_mutex);
  1757. /* If quota was reenabled in the meantime, we have
  1758. * nothing to do */
  1759. if (!sb_has_quota_loaded(sb, cnt)) {
  1760. mutex_lock_nested(&toputinode[cnt]->i_mutex,
  1761. I_MUTEX_QUOTA);
  1762. toputinode[cnt]->i_flags &= ~(S_IMMUTABLE |
  1763. S_NOATIME | S_NOQUOTA);
  1764. truncate_inode_pages(&toputinode[cnt]->i_data,
  1765. 0);
  1766. mutex_unlock(&toputinode[cnt]->i_mutex);
  1767. mark_inode_dirty(toputinode[cnt]);
  1768. }
  1769. mutex_unlock(&dqopt->dqonoff_mutex);
  1770. }
  1771. if (sb->s_bdev)
  1772. invalidate_bdev(sb->s_bdev);
  1773. put_inodes:
  1774. for (cnt = 0; cnt < MAXQUOTAS; cnt++)
  1775. if (toputinode[cnt]) {
  1776. /* On remount RO, we keep the inode pointer so that we
  1777. * can reenable quota on the subsequent remount RW. We
  1778. * have to check 'flags' variable and not use sb_has_
  1779. * function because another quotaon / quotaoff could
  1780. * change global state before we got here. We refuse
  1781. * to suspend quotas when there is pending delete on
  1782. * the quota file... */
  1783. if (!(flags & DQUOT_SUSPENDED))
  1784. iput(toputinode[cnt]);
  1785. else if (!toputinode[cnt]->i_nlink)
  1786. ret = -EBUSY;
  1787. }
  1788. return ret;
  1789. }
  1790. EXPORT_SYMBOL(vfs_quota_disable);
  1791. int vfs_quota_off(struct super_block *sb, int type, int remount)
  1792. {
  1793. return vfs_quota_disable(sb, type, remount ? DQUOT_SUSPENDED :
  1794. (DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED));
  1795. }
  1796. EXPORT_SYMBOL(vfs_quota_off);
  1797. /*
  1798. * Turn quotas on on a device
  1799. */
  1800. /*
  1801. * Helper function to turn quotas on when we already have the inode of
  1802. * quota file and no quota information is loaded.
  1803. */
  1804. static int vfs_load_quota_inode(struct inode *inode, int type, int format_id,
  1805. unsigned int flags)
  1806. {
  1807. struct quota_format_type *fmt = find_quota_format(format_id);
  1808. struct super_block *sb = inode->i_sb;
  1809. struct quota_info *dqopt = sb_dqopt(sb);
  1810. int error;
  1811. int oldflags = -1;
  1812. if (!fmt)
  1813. return -ESRCH;
  1814. if (!S_ISREG(inode->i_mode)) {
  1815. error = -EACCES;
  1816. goto out_fmt;
  1817. }
  1818. if (IS_RDONLY(inode)) {
  1819. error = -EROFS;
  1820. goto out_fmt;
  1821. }
  1822. if (!sb->s_op->quota_write || !sb->s_op->quota_read) {
  1823. error = -EINVAL;
  1824. goto out_fmt;
  1825. }
  1826. /* Usage always has to be set... */
  1827. if (!(flags & DQUOT_USAGE_ENABLED)) {
  1828. error = -EINVAL;
  1829. goto out_fmt;
  1830. }
  1831. if (!(dqopt->flags & DQUOT_QUOTA_SYS_FILE)) {
  1832. /* As we bypass the pagecache we must now flush the inode so
  1833. * that we see all the changes from userspace... */
  1834. write_inode_now(inode, 1);
  1835. /* And now flush the block cache so that kernel sees the
  1836. * changes */
  1837. invalidate_bdev(sb->s_bdev);
  1838. }
  1839. mutex_lock(&dqopt->dqonoff_mutex);
  1840. if (sb_has_quota_loaded(sb, type)) {
  1841. error = -EBUSY;
  1842. goto out_lock;
  1843. }
  1844. if (!(dqopt->flags & DQUOT_QUOTA_SYS_FILE)) {
  1845. /* We don't want quota and atime on quota files (deadlocks
  1846. * possible) Also nobody should write to the file - we use
  1847. * special IO operations which ignore the immutable bit. */
  1848. mutex_lock_nested(&inode->i_mutex, I_MUTEX_QUOTA);
  1849. oldflags = inode->i_flags & (S_NOATIME | S_IMMUTABLE |
  1850. S_NOQUOTA);
  1851. inode->i_flags |= S_NOQUOTA | S_NOATIME | S_IMMUTABLE;
  1852. mutex_unlock(&inode->i_mutex);
  1853. /*
  1854. * When S_NOQUOTA is set, remove dquot references as no more
  1855. * references can be added
  1856. */
  1857. sb->dq_op->drop(inode);
  1858. }
  1859. error = -EIO;
  1860. dqopt->files[type] = igrab(inode);
  1861. if (!dqopt->files[type])
  1862. goto out_lock;
  1863. error = -EINVAL;
  1864. if (!fmt->qf_ops->check_quota_file(sb, type))
  1865. goto out_file_init;
  1866. dqopt->ops[type] = fmt->qf_ops;
  1867. dqopt->info[type].dqi_format = fmt;
  1868. dqopt->info[type].dqi_fmt_id = format_id;
  1869. INIT_LIST_HEAD(&dqopt->info[type].dqi_dirty_list);
  1870. mutex_lock(&dqopt->dqio_mutex);
  1871. error = dqopt->ops[type]->read_file_info(sb, type);
  1872. if (error < 0) {
  1873. mutex_unlock(&dqopt->dqio_mutex);
  1874. goto out_file_init;
  1875. }
  1876. mutex_unlock(&dqopt->dqio_mutex);
  1877. spin_lock(&dq_state_lock);
  1878. dqopt->flags |= dquot_state_flag(flags, type);
  1879. spin_unlock(&dq_state_lock);
  1880. add_dquot_ref(sb, type);
  1881. mutex_unlock(&dqopt->dqonoff_mutex);
  1882. return 0;
  1883. out_file_init:
  1884. dqopt->files[type] = NULL;
  1885. iput(inode);
  1886. out_lock:
  1887. if (oldflags != -1) {
  1888. mutex_lock_nested(&inode->i_mutex, I_MUTEX_QUOTA);
  1889. /* Set the flags back (in the case of accidental quotaon()
  1890. * on a wrong file we don't want to mess up the flags) */
  1891. inode->i_flags &= ~(S_NOATIME | S_NOQUOTA | S_IMMUTABLE);
  1892. inode->i_flags |= oldflags;
  1893. mutex_unlock(&inode->i_mutex);
  1894. }
  1895. mutex_unlock(&dqopt->dqonoff_mutex);
  1896. out_fmt:
  1897. put_quota_format(fmt);
  1898. return error;
  1899. }
  1900. /* Reenable quotas on remount RW */
  1901. static int vfs_quota_on_remount(struct super_block *sb, int type)
  1902. {
  1903. struct quota_info *dqopt = sb_dqopt(sb);
  1904. struct inode *inode;
  1905. int ret;
  1906. unsigned int flags;
  1907. mutex_lock(&dqopt->dqonoff_mutex);
  1908. if (!sb_has_quota_suspended(sb, type)) {
  1909. mutex_unlock(&dqopt->dqonoff_mutex);
  1910. return 0;
  1911. }
  1912. inode = dqopt->files[type];
  1913. dqopt->files[type] = NULL;
  1914. spin_lock(&dq_state_lock);
  1915. flags = dqopt->flags & dquot_state_flag(DQUOT_USAGE_ENABLED |
  1916. DQUOT_LIMITS_ENABLED, type);
  1917. dqopt->flags &= ~dquot_state_flag(DQUOT_STATE_FLAGS, type);
  1918. spin_unlock(&dq_state_lock);
  1919. mutex_unlock(&dqopt->dqonoff_mutex);
  1920. flags = dquot_generic_flag(flags, type);
  1921. ret = vfs_load_quota_inode(inode, type, dqopt->info[type].dqi_fmt_id,
  1922. flags);
  1923. iput(inode);
  1924. return ret;
  1925. }
  1926. int vfs_quota_on_path(struct super_block *sb, int type, int format_id,
  1927. struct path *path)
  1928. {
  1929. int error = security_quota_on(path->dentry);
  1930. if (error)
  1931. return error;
  1932. /* Quota file not on the same filesystem? */
  1933. if (path->mnt->mnt_sb != sb)
  1934. error = -EXDEV;
  1935. else
  1936. error = vfs_load_quota_inode(path->dentry->d_inode, type,
  1937. format_id, DQUOT_USAGE_ENABLED |
  1938. DQUOT_LIMITS_ENABLED);
  1939. return error;
  1940. }
  1941. EXPORT_SYMBOL(vfs_quota_on_path);
  1942. int vfs_quota_on(struct super_block *sb, int type, int format_id, char *name,
  1943. int remount)
  1944. {
  1945. struct path path;
  1946. int error;
  1947. if (remount)
  1948. return vfs_quota_on_remount(sb, type);
  1949. error = kern_path(name, LOOKUP_FOLLOW, &path);
  1950. if (!error) {
  1951. error = vfs_quota_on_path(sb, type, format_id, &path);
  1952. path_put(&path);
  1953. }
  1954. return error;
  1955. }
  1956. EXPORT_SYMBOL(vfs_quota_on);
  1957. /*
  1958. * More powerful function for turning on quotas allowing setting
  1959. * of individual quota flags
  1960. */
  1961. int vfs_quota_enable(struct inode *inode, int type, int format_id,
  1962. unsigned int flags)
  1963. {
  1964. int ret = 0;
  1965. struct super_block *sb = inode->i_sb;
  1966. struct quota_info *dqopt = sb_dqopt(sb);
  1967. /* Just unsuspend quotas? */
  1968. if (flags & DQUOT_SUSPENDED)
  1969. return vfs_quota_on_remount(sb, type);
  1970. if (!flags)
  1971. return 0;
  1972. /* Just updating flags needed? */
  1973. if (sb_has_quota_loaded(sb, type)) {
  1974. mutex_lock(&dqopt->dqonoff_mutex);
  1975. /* Now do a reliable test... */
  1976. if (!sb_has_quota_loaded(sb, type)) {
  1977. mutex_unlock(&dqopt->dqonoff_mutex);
  1978. goto load_quota;
  1979. }
  1980. if (flags & DQUOT_USAGE_ENABLED &&
  1981. sb_has_quota_usage_enabled(sb, type)) {
  1982. ret = -EBUSY;
  1983. goto out_lock;
  1984. }
  1985. if (flags & DQUOT_LIMITS_ENABLED &&
  1986. sb_has_quota_limits_enabled(sb, type)) {
  1987. ret = -EBUSY;
  1988. goto out_lock;
  1989. }
  1990. spin_lock(&dq_state_lock);
  1991. sb_dqopt(sb)->flags |= dquot_state_flag(flags, type);
  1992. spin_unlock(&dq_state_lock);
  1993. out_lock:
  1994. mutex_unlock(&dqopt->dqonoff_mutex);
  1995. return ret;
  1996. }
  1997. load_quota:
  1998. return vfs_load_quota_inode(inode, type, format_id, flags);
  1999. }
  2000. EXPORT_SYMBOL(vfs_quota_enable);
  2001. /*
  2002. * This function is used when filesystem needs to initialize quotas
  2003. * during mount time.
  2004. */
  2005. int vfs_quota_on_mount(struct super_block *sb, char *qf_name,
  2006. int format_id, int type)
  2007. {
  2008. struct dentry *dentry;
  2009. int error;
  2010. mutex_lock(&sb->s_root->d_inode->i_mutex);
  2011. dentry = lookup_one_len(qf_name, sb->s_root, strlen(qf_name));
  2012. mutex_unlock(&sb->s_root->d_inode->i_mutex);
  2013. if (IS_ERR(dentry))
  2014. return PTR_ERR(dentry);
  2015. if (!dentry->d_inode) {
  2016. error = -ENOENT;
  2017. goto out;
  2018. }
  2019. error = security_quota_on(dentry);
  2020. if (!error)
  2021. error = vfs_load_quota_inode(dentry->d_inode, type, format_id,
  2022. DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED);
  2023. out:
  2024. dput(dentry);
  2025. return error;
  2026. }
  2027. EXPORT_SYMBOL(vfs_quota_on_mount);
  2028. /* Wrapper to turn on quotas when remounting rw */
  2029. int vfs_dq_quota_on_remount(struct super_block *sb)
  2030. {
  2031. int cnt;
  2032. int ret = 0, err;
  2033. if (!sb->s_qcop || !sb->s_qcop->quota_on)
  2034. return -ENOSYS;
  2035. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  2036. err = sb->s_qcop->quota_on(sb, cnt, 0, NULL, 1);
  2037. if (err < 0 && !ret)
  2038. ret = err;
  2039. }
  2040. return ret;
  2041. }
  2042. EXPORT_SYMBOL(vfs_dq_quota_on_remount);
  2043. static inline qsize_t qbtos(qsize_t blocks)
  2044. {
  2045. return blocks << QIF_DQBLKSIZE_BITS;
  2046. }
  2047. static inline qsize_t stoqb(qsize_t space)
  2048. {
  2049. return (space + QIF_DQBLKSIZE - 1) >> QIF_DQBLKSIZE_BITS;
  2050. }
  2051. /* Generic routine for getting common part of quota structure */
  2052. static void do_get_dqblk(struct dquot *dquot, struct if_dqblk *di)
  2053. {
  2054. struct mem_dqblk *dm = &dquot->dq_dqb;
  2055. spin_lock(&dq_data_lock);
  2056. di->dqb_bhardlimit = stoqb(dm->dqb_bhardlimit);
  2057. di->dqb_bsoftlimit = stoqb(dm->dqb_bsoftlimit);
  2058. di->dqb_curspace = dm->dqb_curspace + dm->dqb_rsvspace;
  2059. di->dqb_ihardlimit = dm->dqb_ihardlimit;
  2060. di->dqb_isoftlimit = dm->dqb_isoftlimit;
  2061. di->dqb_curinodes = dm->dqb_curinodes;
  2062. di->dqb_btime = dm->dqb_btime;
  2063. di->dqb_itime = dm->dqb_itime;
  2064. di->dqb_valid = QIF_ALL;
  2065. spin_unlock(&dq_data_lock);
  2066. }
  2067. int vfs_get_dqblk(struct super_block *sb, int type, qid_t id,
  2068. struct if_dqblk *di)
  2069. {
  2070. struct dquot *dquot;
  2071. dquot = dqget(sb, id, type);
  2072. if (!dquot)
  2073. return -ESRCH;
  2074. do_get_dqblk(dquot, di);
  2075. dqput(dquot);
  2076. return 0;
  2077. }
  2078. EXPORT_SYMBOL(vfs_get_dqblk);
  2079. /* Generic routine for setting common part of quota structure */
  2080. static int do_set_dqblk(struct dquot *dquot, struct if_dqblk *di)
  2081. {
  2082. struct mem_dqblk *dm = &dquot->dq_dqb;
  2083. int check_blim = 0, check_ilim = 0;
  2084. struct mem_dqinfo *dqi = &sb_dqopt(dquot->dq_sb)->info[dquot->dq_type];
  2085. if ((di->dqb_valid & QIF_BLIMITS &&
  2086. (di->dqb_bhardlimit > dqi->dqi_maxblimit ||
  2087. di->dqb_bsoftlimit > dqi->dqi_maxblimit)) ||
  2088. (di->dqb_valid & QIF_ILIMITS &&
  2089. (di->dqb_ihardlimit > dqi->dqi_maxilimit ||
  2090. di->dqb_isoftlimit > dqi->dqi_maxilimit)))
  2091. return -ERANGE;
  2092. spin_lock(&dq_data_lock);
  2093. if (di->dqb_valid & QIF_SPACE) {
  2094. dm->dqb_curspace = di->dqb_curspace - dm->dqb_rsvspace;
  2095. check_blim = 1;
  2096. __set_bit(DQ_LASTSET_B + QIF_SPACE_B, &dquot->dq_flags);
  2097. }
  2098. if (di->dqb_valid & QIF_BLIMITS) {
  2099. dm->dqb_bsoftlimit = qbtos(di->dqb_bsoftlimit);
  2100. dm->dqb_bhardlimit = qbtos(di->dqb_bhardlimit);
  2101. check_blim = 1;
  2102. __set_bit(DQ_LASTSET_B + QIF_BLIMITS_B, &dquot->dq_flags);
  2103. }
  2104. if (di->dqb_valid & QIF_INODES) {
  2105. dm->dqb_curinodes = di->dqb_curinodes;
  2106. check_ilim = 1;
  2107. __set_bit(DQ_LASTSET_B + QIF_INODES_B, &dquot->dq_flags);
  2108. }
  2109. if (di->dqb_valid & QIF_ILIMITS) {
  2110. dm->dqb_isoftlimit = di->dqb_isoftlimit;
  2111. dm->dqb_ihardlimit = di->dqb_ihardlimit;
  2112. check_ilim = 1;
  2113. __set_bit(DQ_LASTSET_B + QIF_ILIMITS_B, &dquot->dq_flags);
  2114. }
  2115. if (di->dqb_valid & QIF_BTIME) {
  2116. dm->dqb_btime = di->dqb_btime;
  2117. check_blim = 1;
  2118. __set_bit(DQ_LASTSET_B + QIF_BTIME_B, &dquot->dq_flags);
  2119. }
  2120. if (di->dqb_valid & QIF_ITIME) {
  2121. dm->dqb_itime = di->dqb_itime;
  2122. check_ilim = 1;
  2123. __set_bit(DQ_LASTSET_B + QIF_ITIME_B, &dquot->dq_flags);
  2124. }
  2125. if (check_blim) {
  2126. if (!dm->dqb_bsoftlimit ||
  2127. dm->dqb_curspace < dm->dqb_bsoftlimit) {
  2128. dm->dqb_btime = 0;
  2129. clear_bit(DQ_BLKS_B, &dquot->dq_flags);
  2130. } else if (!(di->dqb_valid & QIF_BTIME))
  2131. /* Set grace only if user hasn't provided his own... */
  2132. dm->dqb_btime = get_seconds() + dqi->dqi_bgrace;
  2133. }
  2134. if (check_ilim) {
  2135. if (!dm->dqb_isoftlimit ||
  2136. dm->dqb_curinodes < dm->dqb_isoftlimit) {
  2137. dm->dqb_itime = 0;
  2138. clear_bit(DQ_INODES_B, &dquot->dq_flags);
  2139. } else if (!(di->dqb_valid & QIF_ITIME))
  2140. /* Set grace only if user hasn't provided his own... */
  2141. dm->dqb_itime = get_seconds() + dqi->dqi_igrace;
  2142. }
  2143. if (dm->dqb_bhardlimit || dm->dqb_bsoftlimit || dm->dqb_ihardlimit ||
  2144. dm->dqb_isoftlimit)
  2145. clear_bit(DQ_FAKE_B, &dquot->dq_flags);
  2146. else
  2147. set_bit(DQ_FAKE_B, &dquot->dq_flags);
  2148. spin_unlock(&dq_data_lock);
  2149. mark_dquot_dirty(dquot);
  2150. return 0;
  2151. }
  2152. int vfs_set_dqblk(struct super_block *sb, int type, qid_t id,
  2153. struct if_dqblk *di)
  2154. {
  2155. struct dquot *dquot;
  2156. int rc;
  2157. dquot = dqget(sb, id, type);
  2158. if (!dquot) {
  2159. rc = -ESRCH;
  2160. goto out;
  2161. }
  2162. rc = do_set_dqblk(dquot, di);
  2163. dqput(dquot);
  2164. out:
  2165. return rc;
  2166. }
  2167. EXPORT_SYMBOL(vfs_set_dqblk);
  2168. /* Generic routine for getting common part of quota file information */
  2169. int vfs_get_dqinfo(struct super_block *sb, int type, struct if_dqinfo *ii)
  2170. {
  2171. struct mem_dqinfo *mi;
  2172. mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
  2173. if (!sb_has_quota_active(sb, type)) {
  2174. mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
  2175. return -ESRCH;
  2176. }
  2177. mi = sb_dqopt(sb)->info + type;
  2178. spin_lock(&dq_data_lock);
  2179. ii->dqi_bgrace = mi->dqi_bgrace;
  2180. ii->dqi_igrace = mi->dqi_igrace;
  2181. ii->dqi_flags = mi->dqi_flags & DQF_MASK;
  2182. ii->dqi_valid = IIF_ALL;
  2183. spin_unlock(&dq_data_lock);
  2184. mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
  2185. return 0;
  2186. }
  2187. EXPORT_SYMBOL(vfs_get_dqinfo);
  2188. /* Generic routine for setting common part of quota file information */
  2189. int vfs_set_dqinfo(struct super_block *sb, int type, struct if_dqinfo *ii)
  2190. {
  2191. struct mem_dqinfo *mi;
  2192. int err = 0;
  2193. mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
  2194. if (!sb_has_quota_active(sb, type)) {
  2195. err = -ESRCH;
  2196. goto out;
  2197. }
  2198. mi = sb_dqopt(sb)->info + type;
  2199. spin_lock(&dq_data_lock);
  2200. if (ii->dqi_valid & IIF_BGRACE)
  2201. mi->dqi_bgrace = ii->dqi_bgrace;
  2202. if (ii->dqi_valid & IIF_IGRACE)
  2203. mi->dqi_igrace = ii->dqi_igrace;
  2204. if (ii->dqi_valid & IIF_FLAGS)
  2205. mi->dqi_flags = (mi->dqi_flags & ~DQF_MASK) |
  2206. (ii->dqi_flags & DQF_MASK);
  2207. spin_unlock(&dq_data_lock);
  2208. mark_info_dirty(sb, type);
  2209. /* Force write to disk */
  2210. sb->dq_op->write_info(sb, type);
  2211. out:
  2212. mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
  2213. return err;
  2214. }
  2215. EXPORT_SYMBOL(vfs_set_dqinfo);
  2216. const struct quotactl_ops vfs_quotactl_ops = {
  2217. .quota_on = vfs_quota_on,
  2218. .quota_off = vfs_quota_off,
  2219. .quota_sync = vfs_quota_sync,
  2220. .get_info = vfs_get_dqinfo,
  2221. .set_info = vfs_set_dqinfo,
  2222. .get_dqblk = vfs_get_dqblk,
  2223. .set_dqblk = vfs_set_dqblk
  2224. };
  2225. static ctl_table fs_dqstats_table[] = {
  2226. {
  2227. .procname = "lookups",
  2228. .data = &dqstats.lookups,
  2229. .maxlen = sizeof(int),
  2230. .mode = 0444,
  2231. .proc_handler = proc_dointvec,
  2232. },
  2233. {
  2234. .procname = "drops",
  2235. .data = &dqstats.drops,
  2236. .maxlen = sizeof(int),
  2237. .mode = 0444,
  2238. .proc_handler = proc_dointvec,
  2239. },
  2240. {
  2241. .procname = "reads",
  2242. .data = &dqstats.reads,
  2243. .maxlen = sizeof(int),
  2244. .mode = 0444,
  2245. .proc_handler = proc_dointvec,
  2246. },
  2247. {
  2248. .procname = "writes",
  2249. .data = &dqstats.writes,
  2250. .maxlen = sizeof(int),
  2251. .mode = 0444,
  2252. .proc_handler = proc_dointvec,
  2253. },
  2254. {
  2255. .procname = "cache_hits",
  2256. .data = &dqstats.cache_hits,
  2257. .maxlen = sizeof(int),
  2258. .mode = 0444,
  2259. .proc_handler = proc_dointvec,
  2260. },
  2261. {
  2262. .procname = "allocated_dquots",
  2263. .data = &dqstats.allocated_dquots,
  2264. .maxlen = sizeof(int),
  2265. .mode = 0444,
  2266. .proc_handler = proc_dointvec,
  2267. },
  2268. {
  2269. .procname = "free_dquots",
  2270. .data = &dqstats.free_dquots,
  2271. .maxlen = sizeof(int),
  2272. .mode = 0444,
  2273. .proc_handler = proc_dointvec,
  2274. },
  2275. {
  2276. .procname = "syncs",
  2277. .data = &dqstats.syncs,
  2278. .maxlen = sizeof(int),
  2279. .mode = 0444,
  2280. .proc_handler = proc_dointvec,
  2281. },
  2282. #ifdef CONFIG_PRINT_QUOTA_WARNING
  2283. {
  2284. .procname = "warnings",
  2285. .data = &flag_print_warnings,
  2286. .maxlen = sizeof(int),
  2287. .mode = 0644,
  2288. .proc_handler = proc_dointvec,
  2289. },
  2290. #endif
  2291. { },
  2292. };
  2293. static ctl_table fs_table[] = {
  2294. {
  2295. .procname = "quota",
  2296. .mode = 0555,
  2297. .child = fs_dqstats_table,
  2298. },
  2299. { },
  2300. };
  2301. static ctl_table sys_table[] = {
  2302. {
  2303. .procname = "fs",
  2304. .mode = 0555,
  2305. .child = fs_table,
  2306. },
  2307. { },
  2308. };
  2309. static int __init dquot_init(void)
  2310. {
  2311. int i;
  2312. unsigned long nr_hash, order;
  2313. printk(KERN_NOTICE "VFS: Disk quotas %s\n", __DQUOT_VERSION__);
  2314. register_sysctl_table(sys_table);
  2315. dquot_cachep = kmem_cache_create("dquot",
  2316. sizeof(struct dquot), sizeof(unsigned long) * 4,
  2317. (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
  2318. SLAB_MEM_SPREAD|SLAB_PANIC),
  2319. NULL);
  2320. order = 0;
  2321. dquot_hash = (struct hlist_head *)__get_free_pages(GFP_ATOMIC, order);
  2322. if (!dquot_hash)
  2323. panic("Cannot create dquot hash table");
  2324. /* Find power-of-two hlist_heads which can fit into allocation */
  2325. nr_hash = (1UL << order) * PAGE_SIZE / sizeof(struct hlist_head);
  2326. dq_hash_bits = 0;
  2327. do {
  2328. dq_hash_bits++;
  2329. } while (nr_hash >> dq_hash_bits);
  2330. dq_hash_bits--;
  2331. nr_hash = 1UL << dq_hash_bits;
  2332. dq_hash_mask = nr_hash - 1;
  2333. for (i = 0; i < nr_hash; i++)
  2334. INIT_HLIST_HEAD(dquot_hash + i);
  2335. printk("Dquot-cache hash table entries: %ld (order %ld, %ld bytes)\n",
  2336. nr_hash, order, (PAGE_SIZE << order));
  2337. register_shrinker(&dqcache_shrinker);
  2338. return 0;
  2339. }
  2340. module_init(dquot_init);