dquot.c 62 KB

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