dquot.c 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878
  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/smp_lock.h>
  72. #include <linux/init.h>
  73. #include <linux/module.h>
  74. #include <linux/proc_fs.h>
  75. #include <linux/security.h>
  76. #include <linux/kmod.h>
  77. #include <linux/namei.h>
  78. #include <linux/buffer_head.h>
  79. #include <linux/capability.h>
  80. #include <linux/quotaops.h>
  81. #include <asm/uaccess.h>
  82. #define __DQUOT_PARANOIA
  83. /*
  84. * There are two quota SMP locks. dq_list_lock protects all lists with quotas
  85. * and quota formats and also dqstats structure containing statistics about the
  86. * lists. dq_data_lock protects data from dq_dqb and also mem_dqinfo structures
  87. * and also guards consistency of dquot->dq_dqb with inode->i_blocks, i_bytes.
  88. * i_blocks and i_bytes updates itself are guarded by i_lock acquired directly
  89. * in inode_add_bytes() and inode_sub_bytes().
  90. *
  91. * The spinlock ordering is hence: dq_data_lock > dq_list_lock > i_lock
  92. *
  93. * Note that some things (eg. sb pointer, type, id) doesn't change during
  94. * the life of the dquot structure and so needn't to be protected by a lock
  95. *
  96. * Any operation working on dquots via inode pointers must hold dqptr_sem. If
  97. * operation is just reading pointers from inode (or not using them at all) the
  98. * read lock is enough. If pointers are altered function must hold write lock
  99. * (these locking rules also apply for S_NOQUOTA flag in the inode - note that
  100. * for altering the flag i_mutex is also needed). If operation is holding
  101. * reference to dquot in other way (e.g. quotactl ops) it must be guarded by
  102. * dqonoff_mutex.
  103. * This locking assures that:
  104. * a) update/access to dquot pointers in inode is serialized
  105. * b) everyone is guarded against invalidate_dquots()
  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. * i_mutex on quota files is special (it's below dqio_mutex)
  120. */
  121. static DEFINE_SPINLOCK(dq_list_lock);
  122. DEFINE_SPINLOCK(dq_data_lock);
  123. static char *quotatypes[] = INITQFNAMES;
  124. static struct quota_format_type *quota_formats; /* List of registered formats */
  125. static struct quota_module_name module_names[] = INIT_QUOTA_MODULE_NAMES;
  126. /* SLAB cache for dquot structures */
  127. static kmem_cache_t *dquot_cachep;
  128. int register_quota_format(struct quota_format_type *fmt)
  129. {
  130. spin_lock(&dq_list_lock);
  131. fmt->qf_next = quota_formats;
  132. quota_formats = fmt;
  133. spin_unlock(&dq_list_lock);
  134. return 0;
  135. }
  136. void unregister_quota_format(struct quota_format_type *fmt)
  137. {
  138. struct quota_format_type **actqf;
  139. spin_lock(&dq_list_lock);
  140. for (actqf = &quota_formats; *actqf && *actqf != fmt; actqf = &(*actqf)->qf_next);
  141. if (*actqf)
  142. *actqf = (*actqf)->qf_next;
  143. spin_unlock(&dq_list_lock);
  144. }
  145. static struct quota_format_type *find_quota_format(int id)
  146. {
  147. struct quota_format_type *actqf;
  148. spin_lock(&dq_list_lock);
  149. for (actqf = quota_formats; actqf && actqf->qf_fmt_id != id; actqf = actqf->qf_next);
  150. if (!actqf || !try_module_get(actqf->qf_owner)) {
  151. int qm;
  152. spin_unlock(&dq_list_lock);
  153. for (qm = 0; module_names[qm].qm_fmt_id && module_names[qm].qm_fmt_id != id; qm++);
  154. if (!module_names[qm].qm_fmt_id || request_module(module_names[qm].qm_mod_name))
  155. return NULL;
  156. spin_lock(&dq_list_lock);
  157. for (actqf = quota_formats; actqf && actqf->qf_fmt_id != id; actqf = actqf->qf_next);
  158. if (actqf && !try_module_get(actqf->qf_owner))
  159. actqf = NULL;
  160. }
  161. spin_unlock(&dq_list_lock);
  162. return actqf;
  163. }
  164. static void put_quota_format(struct quota_format_type *fmt)
  165. {
  166. module_put(fmt->qf_owner);
  167. }
  168. /*
  169. * Dquot List Management:
  170. * The quota code uses three lists for dquot management: the inuse_list,
  171. * free_dquots, and dquot_hash[] array. A single dquot structure may be
  172. * on all three lists, depending on its current state.
  173. *
  174. * All dquots are placed to the end of inuse_list when first created, and this
  175. * list is used for invalidate operation, which must look at every dquot.
  176. *
  177. * Unused dquots (dq_count == 0) are added to the free_dquots list when freed,
  178. * and this list is searched whenever we need an available dquot. Dquots are
  179. * removed from the list as soon as they are used again, and
  180. * dqstats.free_dquots gives the number of dquots on the list. When
  181. * dquot is invalidated it's completely released from memory.
  182. *
  183. * Dquots with a specific identity (device, type and id) are placed on
  184. * one of the dquot_hash[] hash chains. The provides an efficient search
  185. * mechanism to locate a specific dquot.
  186. */
  187. static LIST_HEAD(inuse_list);
  188. static LIST_HEAD(free_dquots);
  189. static unsigned int dq_hash_bits, dq_hash_mask;
  190. static struct hlist_head *dquot_hash;
  191. struct dqstats dqstats;
  192. static void dqput(struct dquot *dquot);
  193. static inline unsigned int
  194. hashfn(const struct super_block *sb, unsigned int id, int type)
  195. {
  196. unsigned long tmp;
  197. tmp = (((unsigned long)sb>>L1_CACHE_SHIFT) ^ id) * (MAXQUOTAS - type);
  198. return (tmp + (tmp >> dq_hash_bits)) & dq_hash_mask;
  199. }
  200. /*
  201. * Following list functions expect dq_list_lock to be held
  202. */
  203. static inline void insert_dquot_hash(struct dquot *dquot)
  204. {
  205. struct hlist_head *head = dquot_hash + hashfn(dquot->dq_sb, dquot->dq_id, dquot->dq_type);
  206. hlist_add_head(&dquot->dq_hash, head);
  207. }
  208. static inline void remove_dquot_hash(struct dquot *dquot)
  209. {
  210. hlist_del_init(&dquot->dq_hash);
  211. }
  212. static inline struct dquot *find_dquot(unsigned int hashent, struct super_block *sb, unsigned int id, int type)
  213. {
  214. struct hlist_node *node;
  215. struct dquot *dquot;
  216. hlist_for_each (node, dquot_hash+hashent) {
  217. dquot = hlist_entry(node, struct dquot, dq_hash);
  218. if (dquot->dq_sb == sb && dquot->dq_id == id && dquot->dq_type == type)
  219. return dquot;
  220. }
  221. return NODQUOT;
  222. }
  223. /* Add a dquot to the tail of the free list */
  224. static inline void put_dquot_last(struct dquot *dquot)
  225. {
  226. list_add(&dquot->dq_free, free_dquots.prev);
  227. dqstats.free_dquots++;
  228. }
  229. static inline void remove_free_dquot(struct dquot *dquot)
  230. {
  231. if (list_empty(&dquot->dq_free))
  232. return;
  233. list_del_init(&dquot->dq_free);
  234. dqstats.free_dquots--;
  235. }
  236. static inline void put_inuse(struct dquot *dquot)
  237. {
  238. /* We add to the back of inuse list so we don't have to restart
  239. * when traversing this list and we block */
  240. list_add(&dquot->dq_inuse, inuse_list.prev);
  241. dqstats.allocated_dquots++;
  242. }
  243. static inline void remove_inuse(struct dquot *dquot)
  244. {
  245. dqstats.allocated_dquots--;
  246. list_del(&dquot->dq_inuse);
  247. }
  248. /*
  249. * End of list functions needing dq_list_lock
  250. */
  251. static void wait_on_dquot(struct dquot *dquot)
  252. {
  253. mutex_lock(&dquot->dq_lock);
  254. mutex_unlock(&dquot->dq_lock);
  255. }
  256. #define mark_dquot_dirty(dquot) ((dquot)->dq_sb->dq_op->mark_dirty(dquot))
  257. int dquot_mark_dquot_dirty(struct dquot *dquot)
  258. {
  259. spin_lock(&dq_list_lock);
  260. if (!test_and_set_bit(DQ_MOD_B, &dquot->dq_flags))
  261. list_add(&dquot->dq_dirty, &sb_dqopt(dquot->dq_sb)->
  262. info[dquot->dq_type].dqi_dirty_list);
  263. spin_unlock(&dq_list_lock);
  264. return 0;
  265. }
  266. /* This function needs dq_list_lock */
  267. static inline int clear_dquot_dirty(struct dquot *dquot)
  268. {
  269. if (!test_and_clear_bit(DQ_MOD_B, &dquot->dq_flags))
  270. return 0;
  271. list_del_init(&dquot->dq_dirty);
  272. return 1;
  273. }
  274. void mark_info_dirty(struct super_block *sb, int type)
  275. {
  276. set_bit(DQF_INFO_DIRTY_B, &sb_dqopt(sb)->info[type].dqi_flags);
  277. }
  278. EXPORT_SYMBOL(mark_info_dirty);
  279. /*
  280. * Read dquot from disk and alloc space for it
  281. */
  282. int dquot_acquire(struct dquot *dquot)
  283. {
  284. int ret = 0, ret2 = 0;
  285. struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
  286. mutex_lock(&dquot->dq_lock);
  287. mutex_lock(&dqopt->dqio_mutex);
  288. if (!test_bit(DQ_READ_B, &dquot->dq_flags))
  289. ret = dqopt->ops[dquot->dq_type]->read_dqblk(dquot);
  290. if (ret < 0)
  291. goto out_iolock;
  292. set_bit(DQ_READ_B, &dquot->dq_flags);
  293. /* Instantiate dquot if needed */
  294. if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags) && !dquot->dq_off) {
  295. ret = dqopt->ops[dquot->dq_type]->commit_dqblk(dquot);
  296. /* Write the info if needed */
  297. if (info_dirty(&dqopt->info[dquot->dq_type]))
  298. ret2 = dqopt->ops[dquot->dq_type]->write_file_info(dquot->dq_sb, dquot->dq_type);
  299. if (ret < 0)
  300. goto out_iolock;
  301. if (ret2 < 0) {
  302. ret = ret2;
  303. goto out_iolock;
  304. }
  305. }
  306. set_bit(DQ_ACTIVE_B, &dquot->dq_flags);
  307. out_iolock:
  308. mutex_unlock(&dqopt->dqio_mutex);
  309. mutex_unlock(&dquot->dq_lock);
  310. return ret;
  311. }
  312. /*
  313. * Write dquot to disk
  314. */
  315. int dquot_commit(struct dquot *dquot)
  316. {
  317. int ret = 0, ret2 = 0;
  318. struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
  319. mutex_lock(&dqopt->dqio_mutex);
  320. spin_lock(&dq_list_lock);
  321. if (!clear_dquot_dirty(dquot)) {
  322. spin_unlock(&dq_list_lock);
  323. goto out_sem;
  324. }
  325. spin_unlock(&dq_list_lock);
  326. /* Inactive dquot can be only if there was error during read/init
  327. * => we have better not writing it */
  328. if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
  329. ret = dqopt->ops[dquot->dq_type]->commit_dqblk(dquot);
  330. if (info_dirty(&dqopt->info[dquot->dq_type]))
  331. ret2 = dqopt->ops[dquot->dq_type]->write_file_info(dquot->dq_sb, dquot->dq_type);
  332. if (ret >= 0)
  333. ret = ret2;
  334. }
  335. out_sem:
  336. mutex_unlock(&dqopt->dqio_mutex);
  337. return ret;
  338. }
  339. /*
  340. * Release dquot
  341. */
  342. int dquot_release(struct dquot *dquot)
  343. {
  344. int ret = 0, ret2 = 0;
  345. struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
  346. mutex_lock(&dquot->dq_lock);
  347. /* Check whether we are not racing with some other dqget() */
  348. if (atomic_read(&dquot->dq_count) > 1)
  349. goto out_dqlock;
  350. mutex_lock(&dqopt->dqio_mutex);
  351. if (dqopt->ops[dquot->dq_type]->release_dqblk) {
  352. ret = dqopt->ops[dquot->dq_type]->release_dqblk(dquot);
  353. /* Write the info */
  354. if (info_dirty(&dqopt->info[dquot->dq_type]))
  355. ret2 = dqopt->ops[dquot->dq_type]->write_file_info(dquot->dq_sb, dquot->dq_type);
  356. if (ret >= 0)
  357. ret = ret2;
  358. }
  359. clear_bit(DQ_ACTIVE_B, &dquot->dq_flags);
  360. mutex_unlock(&dqopt->dqio_mutex);
  361. out_dqlock:
  362. mutex_unlock(&dquot->dq_lock);
  363. return ret;
  364. }
  365. /* Invalidate all dquots on the list. Note that this function is called after
  366. * quota is disabled and pointers from inodes removed so there cannot be new
  367. * quota users. There can still be some users of quotas due to inodes being
  368. * just deleted or pruned by prune_icache() (those are not attached to any
  369. * list). We have to wait for such users.
  370. */
  371. static void invalidate_dquots(struct super_block *sb, int type)
  372. {
  373. struct dquot *dquot, *tmp;
  374. restart:
  375. spin_lock(&dq_list_lock);
  376. list_for_each_entry_safe(dquot, tmp, &inuse_list, dq_inuse) {
  377. if (dquot->dq_sb != sb)
  378. continue;
  379. if (dquot->dq_type != type)
  380. continue;
  381. /* Wait for dquot users */
  382. if (atomic_read(&dquot->dq_count)) {
  383. DEFINE_WAIT(wait);
  384. atomic_inc(&dquot->dq_count);
  385. prepare_to_wait(&dquot->dq_wait_unused, &wait,
  386. TASK_UNINTERRUPTIBLE);
  387. spin_unlock(&dq_list_lock);
  388. /* Once dqput() wakes us up, we know it's time to free
  389. * the dquot.
  390. * IMPORTANT: we rely on the fact that there is always
  391. * at most one process waiting for dquot to free.
  392. * Otherwise dq_count would be > 1 and we would never
  393. * wake up.
  394. */
  395. if (atomic_read(&dquot->dq_count) > 1)
  396. schedule();
  397. finish_wait(&dquot->dq_wait_unused, &wait);
  398. dqput(dquot);
  399. /* At this moment dquot() need not exist (it could be
  400. * reclaimed by prune_dqcache(). Hence we must
  401. * restart. */
  402. goto restart;
  403. }
  404. /*
  405. * Quota now has no users and it has been written on last
  406. * dqput()
  407. */
  408. remove_dquot_hash(dquot);
  409. remove_free_dquot(dquot);
  410. remove_inuse(dquot);
  411. kmem_cache_free(dquot_cachep, dquot);
  412. }
  413. spin_unlock(&dq_list_lock);
  414. }
  415. int vfs_quota_sync(struct super_block *sb, int type)
  416. {
  417. struct list_head *dirty;
  418. struct dquot *dquot;
  419. struct quota_info *dqopt = sb_dqopt(sb);
  420. int cnt;
  421. mutex_lock(&dqopt->dqonoff_mutex);
  422. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  423. if (type != -1 && cnt != type)
  424. continue;
  425. if (!sb_has_quota_enabled(sb, cnt))
  426. continue;
  427. spin_lock(&dq_list_lock);
  428. dirty = &dqopt->info[cnt].dqi_dirty_list;
  429. while (!list_empty(dirty)) {
  430. dquot = list_entry(dirty->next, struct dquot, dq_dirty);
  431. /* Dirty and inactive can be only bad dquot... */
  432. if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
  433. clear_dquot_dirty(dquot);
  434. continue;
  435. }
  436. /* Now we have active dquot from which someone is
  437. * holding reference so we can safely just increase
  438. * use count */
  439. atomic_inc(&dquot->dq_count);
  440. dqstats.lookups++;
  441. spin_unlock(&dq_list_lock);
  442. sb->dq_op->write_dquot(dquot);
  443. dqput(dquot);
  444. spin_lock(&dq_list_lock);
  445. }
  446. spin_unlock(&dq_list_lock);
  447. }
  448. for (cnt = 0; cnt < MAXQUOTAS; cnt++)
  449. if ((cnt == type || type == -1) && sb_has_quota_enabled(sb, cnt)
  450. && info_dirty(&dqopt->info[cnt]))
  451. sb->dq_op->write_info(sb, cnt);
  452. spin_lock(&dq_list_lock);
  453. dqstats.syncs++;
  454. spin_unlock(&dq_list_lock);
  455. mutex_unlock(&dqopt->dqonoff_mutex);
  456. return 0;
  457. }
  458. /* Free unused dquots from cache */
  459. static void prune_dqcache(int count)
  460. {
  461. struct list_head *head;
  462. struct dquot *dquot;
  463. head = free_dquots.prev;
  464. while (head != &free_dquots && count) {
  465. dquot = list_entry(head, struct dquot, dq_free);
  466. remove_dquot_hash(dquot);
  467. remove_free_dquot(dquot);
  468. remove_inuse(dquot);
  469. kmem_cache_free(dquot_cachep, dquot);
  470. count--;
  471. head = free_dquots.prev;
  472. }
  473. }
  474. /*
  475. * This is called from kswapd when we think we need some
  476. * more memory
  477. */
  478. static int shrink_dqcache_memory(int nr, gfp_t gfp_mask)
  479. {
  480. if (nr) {
  481. spin_lock(&dq_list_lock);
  482. prune_dqcache(nr);
  483. spin_unlock(&dq_list_lock);
  484. }
  485. return (dqstats.free_dquots / 100) * sysctl_vfs_cache_pressure;
  486. }
  487. /*
  488. * Put reference to dquot
  489. * NOTE: If you change this function please check whether dqput_blocks() works right...
  490. * MUST be called with either dqptr_sem or dqonoff_mutex held
  491. */
  492. static void dqput(struct dquot *dquot)
  493. {
  494. if (!dquot)
  495. return;
  496. #ifdef __DQUOT_PARANOIA
  497. if (!atomic_read(&dquot->dq_count)) {
  498. printk("VFS: dqput: trying to free free dquot\n");
  499. printk("VFS: device %s, dquot of %s %d\n",
  500. dquot->dq_sb->s_id,
  501. quotatypes[dquot->dq_type],
  502. dquot->dq_id);
  503. BUG();
  504. }
  505. #endif
  506. spin_lock(&dq_list_lock);
  507. dqstats.drops++;
  508. spin_unlock(&dq_list_lock);
  509. we_slept:
  510. spin_lock(&dq_list_lock);
  511. if (atomic_read(&dquot->dq_count) > 1) {
  512. /* We have more than one user... nothing to do */
  513. atomic_dec(&dquot->dq_count);
  514. /* Releasing dquot during quotaoff phase? */
  515. if (!sb_has_quota_enabled(dquot->dq_sb, dquot->dq_type) &&
  516. atomic_read(&dquot->dq_count) == 1)
  517. wake_up(&dquot->dq_wait_unused);
  518. spin_unlock(&dq_list_lock);
  519. return;
  520. }
  521. /* Need to release dquot? */
  522. if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags) && dquot_dirty(dquot)) {
  523. spin_unlock(&dq_list_lock);
  524. /* Commit dquot before releasing */
  525. dquot->dq_sb->dq_op->write_dquot(dquot);
  526. goto we_slept;
  527. }
  528. /* Clear flag in case dquot was inactive (something bad happened) */
  529. clear_dquot_dirty(dquot);
  530. if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
  531. spin_unlock(&dq_list_lock);
  532. dquot->dq_sb->dq_op->release_dquot(dquot);
  533. goto we_slept;
  534. }
  535. atomic_dec(&dquot->dq_count);
  536. #ifdef __DQUOT_PARANOIA
  537. /* sanity check */
  538. if (!list_empty(&dquot->dq_free))
  539. BUG();
  540. #endif
  541. put_dquot_last(dquot);
  542. spin_unlock(&dq_list_lock);
  543. }
  544. static struct dquot *get_empty_dquot(struct super_block *sb, int type)
  545. {
  546. struct dquot *dquot;
  547. dquot = kmem_cache_alloc(dquot_cachep, SLAB_NOFS);
  548. if(!dquot)
  549. return NODQUOT;
  550. memset((caddr_t)dquot, 0, sizeof(struct dquot));
  551. mutex_init(&dquot->dq_lock);
  552. INIT_LIST_HEAD(&dquot->dq_free);
  553. INIT_LIST_HEAD(&dquot->dq_inuse);
  554. INIT_HLIST_NODE(&dquot->dq_hash);
  555. INIT_LIST_HEAD(&dquot->dq_dirty);
  556. init_waitqueue_head(&dquot->dq_wait_unused);
  557. dquot->dq_sb = sb;
  558. dquot->dq_type = type;
  559. atomic_set(&dquot->dq_count, 1);
  560. return dquot;
  561. }
  562. /*
  563. * Get reference to dquot
  564. * MUST be called with either dqptr_sem or dqonoff_mutex held
  565. */
  566. static struct dquot *dqget(struct super_block *sb, unsigned int id, int type)
  567. {
  568. unsigned int hashent = hashfn(sb, id, type);
  569. struct dquot *dquot, *empty = NODQUOT;
  570. if (!sb_has_quota_enabled(sb, type))
  571. return NODQUOT;
  572. we_slept:
  573. spin_lock(&dq_list_lock);
  574. if ((dquot = find_dquot(hashent, sb, id, type)) == NODQUOT) {
  575. if (empty == NODQUOT) {
  576. spin_unlock(&dq_list_lock);
  577. if ((empty = get_empty_dquot(sb, type)) == NODQUOT)
  578. schedule(); /* Try to wait for a moment... */
  579. goto we_slept;
  580. }
  581. dquot = empty;
  582. dquot->dq_id = id;
  583. /* all dquots go on the inuse_list */
  584. put_inuse(dquot);
  585. /* hash it first so it can be found */
  586. insert_dquot_hash(dquot);
  587. dqstats.lookups++;
  588. spin_unlock(&dq_list_lock);
  589. } else {
  590. if (!atomic_read(&dquot->dq_count))
  591. remove_free_dquot(dquot);
  592. atomic_inc(&dquot->dq_count);
  593. dqstats.cache_hits++;
  594. dqstats.lookups++;
  595. spin_unlock(&dq_list_lock);
  596. if (empty)
  597. kmem_cache_free(dquot_cachep, empty);
  598. }
  599. /* Wait for dq_lock - after this we know that either dquot_release() is already
  600. * finished or it will be canceled due to dq_count > 1 test */
  601. wait_on_dquot(dquot);
  602. /* Read the dquot and instantiate it (everything done only if needed) */
  603. if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags) && sb->dq_op->acquire_dquot(dquot) < 0) {
  604. dqput(dquot);
  605. return NODQUOT;
  606. }
  607. #ifdef __DQUOT_PARANOIA
  608. if (!dquot->dq_sb) /* Has somebody invalidated entry under us? */
  609. BUG();
  610. #endif
  611. return dquot;
  612. }
  613. static int dqinit_needed(struct inode *inode, int type)
  614. {
  615. int cnt;
  616. if (IS_NOQUOTA(inode))
  617. return 0;
  618. if (type != -1)
  619. return inode->i_dquot[type] == NODQUOT;
  620. for (cnt = 0; cnt < MAXQUOTAS; cnt++)
  621. if (inode->i_dquot[cnt] == NODQUOT)
  622. return 1;
  623. return 0;
  624. }
  625. /* This routine is guarded by dqonoff_mutex mutex */
  626. static void add_dquot_ref(struct super_block *sb, int type)
  627. {
  628. struct list_head *p;
  629. restart:
  630. file_list_lock();
  631. list_for_each(p, &sb->s_files) {
  632. struct file *filp = list_entry(p, struct file, f_u.fu_list);
  633. struct inode *inode = filp->f_dentry->d_inode;
  634. if (filp->f_mode & FMODE_WRITE && dqinit_needed(inode, type)) {
  635. struct dentry *dentry = dget(filp->f_dentry);
  636. file_list_unlock();
  637. sb->dq_op->initialize(inode, type);
  638. dput(dentry);
  639. /* As we may have blocked we had better restart... */
  640. goto restart;
  641. }
  642. }
  643. file_list_unlock();
  644. }
  645. /* Return 0 if dqput() won't block (note that 1 doesn't necessarily mean blocking) */
  646. static inline int dqput_blocks(struct dquot *dquot)
  647. {
  648. if (atomic_read(&dquot->dq_count) <= 1)
  649. return 1;
  650. return 0;
  651. }
  652. /* Remove references to dquots from inode - add dquot to list for freeing if needed */
  653. /* We can't race with anybody because we hold dqptr_sem for writing... */
  654. int remove_inode_dquot_ref(struct inode *inode, int type, struct list_head *tofree_head)
  655. {
  656. struct dquot *dquot = inode->i_dquot[type];
  657. inode->i_dquot[type] = NODQUOT;
  658. if (dquot != NODQUOT) {
  659. if (dqput_blocks(dquot)) {
  660. #ifdef __DQUOT_PARANOIA
  661. if (atomic_read(&dquot->dq_count) != 1)
  662. printk(KERN_WARNING "VFS: Adding dquot with dq_count %d to dispose list.\n", atomic_read(&dquot->dq_count));
  663. #endif
  664. spin_lock(&dq_list_lock);
  665. list_add(&dquot->dq_free, tofree_head); /* As dquot must have currently users it can't be on the free list... */
  666. spin_unlock(&dq_list_lock);
  667. return 1;
  668. }
  669. else
  670. dqput(dquot); /* We have guaranteed we won't block */
  671. }
  672. return 0;
  673. }
  674. /* Free list of dquots - called from inode.c */
  675. /* dquots are removed from inodes, no new references can be got so we are the only ones holding reference */
  676. static void put_dquot_list(struct list_head *tofree_head)
  677. {
  678. struct list_head *act_head;
  679. struct dquot *dquot;
  680. act_head = tofree_head->next;
  681. /* So now we have dquots on the list... Just free them */
  682. while (act_head != tofree_head) {
  683. dquot = list_entry(act_head, struct dquot, dq_free);
  684. act_head = act_head->next;
  685. list_del_init(&dquot->dq_free); /* Remove dquot from the list so we won't have problems... */
  686. dqput(dquot);
  687. }
  688. }
  689. /* Gather all references from inodes and drop them */
  690. static void drop_dquot_ref(struct super_block *sb, int type)
  691. {
  692. LIST_HEAD(tofree_head);
  693. down_write(&sb_dqopt(sb)->dqptr_sem);
  694. remove_dquot_ref(sb, type, &tofree_head);
  695. up_write(&sb_dqopt(sb)->dqptr_sem);
  696. put_dquot_list(&tofree_head);
  697. }
  698. static inline void dquot_incr_inodes(struct dquot *dquot, unsigned long number)
  699. {
  700. dquot->dq_dqb.dqb_curinodes += number;
  701. }
  702. static inline void dquot_incr_space(struct dquot *dquot, qsize_t number)
  703. {
  704. dquot->dq_dqb.dqb_curspace += number;
  705. }
  706. static inline void dquot_decr_inodes(struct dquot *dquot, unsigned long number)
  707. {
  708. if (dquot->dq_dqb.dqb_curinodes > number)
  709. dquot->dq_dqb.dqb_curinodes -= number;
  710. else
  711. dquot->dq_dqb.dqb_curinodes = 0;
  712. if (dquot->dq_dqb.dqb_curinodes <= dquot->dq_dqb.dqb_isoftlimit)
  713. dquot->dq_dqb.dqb_itime = (time_t) 0;
  714. clear_bit(DQ_INODES_B, &dquot->dq_flags);
  715. }
  716. static inline void dquot_decr_space(struct dquot *dquot, qsize_t number)
  717. {
  718. if (dquot->dq_dqb.dqb_curspace > number)
  719. dquot->dq_dqb.dqb_curspace -= number;
  720. else
  721. dquot->dq_dqb.dqb_curspace = 0;
  722. if (toqb(dquot->dq_dqb.dqb_curspace) <= dquot->dq_dqb.dqb_bsoftlimit)
  723. dquot->dq_dqb.dqb_btime = (time_t) 0;
  724. clear_bit(DQ_BLKS_B, &dquot->dq_flags);
  725. }
  726. static int flag_print_warnings = 1;
  727. static inline int need_print_warning(struct dquot *dquot)
  728. {
  729. if (!flag_print_warnings)
  730. return 0;
  731. switch (dquot->dq_type) {
  732. case USRQUOTA:
  733. return current->fsuid == dquot->dq_id;
  734. case GRPQUOTA:
  735. return in_group_p(dquot->dq_id);
  736. }
  737. return 0;
  738. }
  739. /* Values of warnings */
  740. #define NOWARN 0
  741. #define IHARDWARN 1
  742. #define ISOFTLONGWARN 2
  743. #define ISOFTWARN 3
  744. #define BHARDWARN 4
  745. #define BSOFTLONGWARN 5
  746. #define BSOFTWARN 6
  747. /* Print warning to user which exceeded quota */
  748. static void print_warning(struct dquot *dquot, const char warntype)
  749. {
  750. char *msg = NULL;
  751. int flag = (warntype == BHARDWARN || warntype == BSOFTLONGWARN) ? DQ_BLKS_B :
  752. ((warntype == IHARDWARN || warntype == ISOFTLONGWARN) ? DQ_INODES_B : 0);
  753. if (!need_print_warning(dquot) || (flag && test_and_set_bit(flag, &dquot->dq_flags)))
  754. return;
  755. tty_write_message(current->signal->tty, dquot->dq_sb->s_id);
  756. if (warntype == ISOFTWARN || warntype == BSOFTWARN)
  757. tty_write_message(current->signal->tty, ": warning, ");
  758. else
  759. tty_write_message(current->signal->tty, ": write failed, ");
  760. tty_write_message(current->signal->tty, quotatypes[dquot->dq_type]);
  761. switch (warntype) {
  762. case IHARDWARN:
  763. msg = " file limit reached.\r\n";
  764. break;
  765. case ISOFTLONGWARN:
  766. msg = " file quota exceeded too long.\r\n";
  767. break;
  768. case ISOFTWARN:
  769. msg = " file quota exceeded.\r\n";
  770. break;
  771. case BHARDWARN:
  772. msg = " block limit reached.\r\n";
  773. break;
  774. case BSOFTLONGWARN:
  775. msg = " block quota exceeded too long.\r\n";
  776. break;
  777. case BSOFTWARN:
  778. msg = " block quota exceeded.\r\n";
  779. break;
  780. }
  781. tty_write_message(current->signal->tty, msg);
  782. }
  783. static inline void flush_warnings(struct dquot **dquots, char *warntype)
  784. {
  785. int i;
  786. for (i = 0; i < MAXQUOTAS; i++)
  787. if (dquots[i] != NODQUOT && warntype[i] != NOWARN)
  788. print_warning(dquots[i], warntype[i]);
  789. }
  790. static inline char ignore_hardlimit(struct dquot *dquot)
  791. {
  792. struct mem_dqinfo *info = &sb_dqopt(dquot->dq_sb)->info[dquot->dq_type];
  793. return capable(CAP_SYS_RESOURCE) &&
  794. (info->dqi_format->qf_fmt_id != QFMT_VFS_OLD || !(info->dqi_flags & V1_DQF_RSQUASH));
  795. }
  796. /* needs dq_data_lock */
  797. static int check_idq(struct dquot *dquot, ulong inodes, char *warntype)
  798. {
  799. *warntype = NOWARN;
  800. if (inodes <= 0 || test_bit(DQ_FAKE_B, &dquot->dq_flags))
  801. return QUOTA_OK;
  802. if (dquot->dq_dqb.dqb_ihardlimit &&
  803. (dquot->dq_dqb.dqb_curinodes + inodes) > dquot->dq_dqb.dqb_ihardlimit &&
  804. !ignore_hardlimit(dquot)) {
  805. *warntype = IHARDWARN;
  806. return NO_QUOTA;
  807. }
  808. if (dquot->dq_dqb.dqb_isoftlimit &&
  809. (dquot->dq_dqb.dqb_curinodes + inodes) > dquot->dq_dqb.dqb_isoftlimit &&
  810. dquot->dq_dqb.dqb_itime && get_seconds() >= dquot->dq_dqb.dqb_itime &&
  811. !ignore_hardlimit(dquot)) {
  812. *warntype = ISOFTLONGWARN;
  813. return NO_QUOTA;
  814. }
  815. if (dquot->dq_dqb.dqb_isoftlimit &&
  816. (dquot->dq_dqb.dqb_curinodes + inodes) > dquot->dq_dqb.dqb_isoftlimit &&
  817. dquot->dq_dqb.dqb_itime == 0) {
  818. *warntype = ISOFTWARN;
  819. dquot->dq_dqb.dqb_itime = get_seconds() + sb_dqopt(dquot->dq_sb)->info[dquot->dq_type].dqi_igrace;
  820. }
  821. return QUOTA_OK;
  822. }
  823. /* needs dq_data_lock */
  824. static int check_bdq(struct dquot *dquot, qsize_t space, int prealloc, char *warntype)
  825. {
  826. *warntype = 0;
  827. if (space <= 0 || test_bit(DQ_FAKE_B, &dquot->dq_flags))
  828. return QUOTA_OK;
  829. if (dquot->dq_dqb.dqb_bhardlimit &&
  830. toqb(dquot->dq_dqb.dqb_curspace + space) > dquot->dq_dqb.dqb_bhardlimit &&
  831. !ignore_hardlimit(dquot)) {
  832. if (!prealloc)
  833. *warntype = BHARDWARN;
  834. return NO_QUOTA;
  835. }
  836. if (dquot->dq_dqb.dqb_bsoftlimit &&
  837. toqb(dquot->dq_dqb.dqb_curspace + space) > dquot->dq_dqb.dqb_bsoftlimit &&
  838. dquot->dq_dqb.dqb_btime && get_seconds() >= dquot->dq_dqb.dqb_btime &&
  839. !ignore_hardlimit(dquot)) {
  840. if (!prealloc)
  841. *warntype = BSOFTLONGWARN;
  842. return NO_QUOTA;
  843. }
  844. if (dquot->dq_dqb.dqb_bsoftlimit &&
  845. toqb(dquot->dq_dqb.dqb_curspace + space) > dquot->dq_dqb.dqb_bsoftlimit &&
  846. dquot->dq_dqb.dqb_btime == 0) {
  847. if (!prealloc) {
  848. *warntype = BSOFTWARN;
  849. dquot->dq_dqb.dqb_btime = get_seconds() + sb_dqopt(dquot->dq_sb)->info[dquot->dq_type].dqi_bgrace;
  850. }
  851. else
  852. /*
  853. * We don't allow preallocation to exceed softlimit so exceeding will
  854. * be always printed
  855. */
  856. return NO_QUOTA;
  857. }
  858. return QUOTA_OK;
  859. }
  860. /*
  861. * Initialize quota pointers in inode
  862. * Transaction must be started at entry
  863. */
  864. int dquot_initialize(struct inode *inode, int type)
  865. {
  866. unsigned int id = 0;
  867. int cnt, ret = 0;
  868. /* First test before acquiring mutex - solves deadlocks when we
  869. * re-enter the quota code and are already holding the mutex */
  870. if (IS_NOQUOTA(inode))
  871. return 0;
  872. down_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
  873. /* Having dqptr_sem we know NOQUOTA flags can't be altered... */
  874. if (IS_NOQUOTA(inode))
  875. goto out_err;
  876. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  877. if (type != -1 && cnt != type)
  878. continue;
  879. if (inode->i_dquot[cnt] == NODQUOT) {
  880. switch (cnt) {
  881. case USRQUOTA:
  882. id = inode->i_uid;
  883. break;
  884. case GRPQUOTA:
  885. id = inode->i_gid;
  886. break;
  887. }
  888. inode->i_dquot[cnt] = dqget(inode->i_sb, id, cnt);
  889. }
  890. }
  891. out_err:
  892. up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
  893. return ret;
  894. }
  895. /*
  896. * Release all quotas referenced by inode
  897. * Transaction must be started at an entry
  898. */
  899. int dquot_drop(struct inode *inode)
  900. {
  901. int cnt;
  902. down_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
  903. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  904. if (inode->i_dquot[cnt] != NODQUOT) {
  905. dqput(inode->i_dquot[cnt]);
  906. inode->i_dquot[cnt] = NODQUOT;
  907. }
  908. }
  909. up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
  910. return 0;
  911. }
  912. /*
  913. * Following four functions update i_blocks+i_bytes fields and
  914. * quota information (together with appropriate checks)
  915. * NOTE: We absolutely rely on the fact that caller dirties
  916. * the inode (usually macros in quotaops.h care about this) and
  917. * holds a handle for the current transaction so that dquot write and
  918. * inode write go into the same transaction.
  919. */
  920. /*
  921. * This operation can block, but only after everything is updated
  922. */
  923. int dquot_alloc_space(struct inode *inode, qsize_t number, int warn)
  924. {
  925. int cnt, ret = NO_QUOTA;
  926. char warntype[MAXQUOTAS];
  927. /* First test before acquiring mutex - solves deadlocks when we
  928. * re-enter the quota code and are already holding the mutex */
  929. if (IS_NOQUOTA(inode)) {
  930. out_add:
  931. inode_add_bytes(inode, number);
  932. return QUOTA_OK;
  933. }
  934. for (cnt = 0; cnt < MAXQUOTAS; cnt++)
  935. warntype[cnt] = NOWARN;
  936. down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  937. if (IS_NOQUOTA(inode)) { /* Now we can do reliable test... */
  938. up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  939. goto out_add;
  940. }
  941. spin_lock(&dq_data_lock);
  942. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  943. if (inode->i_dquot[cnt] == NODQUOT)
  944. continue;
  945. if (check_bdq(inode->i_dquot[cnt], number, warn, warntype+cnt) == NO_QUOTA)
  946. goto warn_put_all;
  947. }
  948. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  949. if (inode->i_dquot[cnt] == NODQUOT)
  950. continue;
  951. dquot_incr_space(inode->i_dquot[cnt], number);
  952. }
  953. inode_add_bytes(inode, number);
  954. ret = QUOTA_OK;
  955. warn_put_all:
  956. spin_unlock(&dq_data_lock);
  957. if (ret == QUOTA_OK)
  958. /* Dirtify all the dquots - this can block when journalling */
  959. for (cnt = 0; cnt < MAXQUOTAS; cnt++)
  960. if (inode->i_dquot[cnt])
  961. mark_dquot_dirty(inode->i_dquot[cnt]);
  962. flush_warnings(inode->i_dquot, warntype);
  963. up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  964. return ret;
  965. }
  966. /*
  967. * This operation can block, but only after everything is updated
  968. */
  969. int dquot_alloc_inode(const struct inode *inode, unsigned long number)
  970. {
  971. int cnt, ret = NO_QUOTA;
  972. char warntype[MAXQUOTAS];
  973. /* First test before acquiring mutex - solves deadlocks when we
  974. * re-enter the quota code and are already holding the mutex */
  975. if (IS_NOQUOTA(inode))
  976. return QUOTA_OK;
  977. for (cnt = 0; cnt < MAXQUOTAS; cnt++)
  978. warntype[cnt] = NOWARN;
  979. down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  980. if (IS_NOQUOTA(inode)) {
  981. up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  982. return QUOTA_OK;
  983. }
  984. spin_lock(&dq_data_lock);
  985. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  986. if (inode->i_dquot[cnt] == NODQUOT)
  987. continue;
  988. if (check_idq(inode->i_dquot[cnt], number, warntype+cnt) == NO_QUOTA)
  989. goto warn_put_all;
  990. }
  991. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  992. if (inode->i_dquot[cnt] == NODQUOT)
  993. continue;
  994. dquot_incr_inodes(inode->i_dquot[cnt], number);
  995. }
  996. ret = QUOTA_OK;
  997. warn_put_all:
  998. spin_unlock(&dq_data_lock);
  999. if (ret == QUOTA_OK)
  1000. /* Dirtify all the dquots - this can block when journalling */
  1001. for (cnt = 0; cnt < MAXQUOTAS; cnt++)
  1002. if (inode->i_dquot[cnt])
  1003. mark_dquot_dirty(inode->i_dquot[cnt]);
  1004. flush_warnings((struct dquot **)inode->i_dquot, warntype);
  1005. up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1006. return ret;
  1007. }
  1008. /*
  1009. * This operation can block, but only after everything is updated
  1010. */
  1011. int dquot_free_space(struct inode *inode, qsize_t number)
  1012. {
  1013. unsigned int cnt;
  1014. /* First test before acquiring mutex - solves deadlocks when we
  1015. * re-enter the quota code and are already holding the mutex */
  1016. if (IS_NOQUOTA(inode)) {
  1017. out_sub:
  1018. inode_sub_bytes(inode, number);
  1019. return QUOTA_OK;
  1020. }
  1021. down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1022. /* Now recheck reliably when holding dqptr_sem */
  1023. if (IS_NOQUOTA(inode)) {
  1024. up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1025. goto out_sub;
  1026. }
  1027. spin_lock(&dq_data_lock);
  1028. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1029. if (inode->i_dquot[cnt] == NODQUOT)
  1030. continue;
  1031. dquot_decr_space(inode->i_dquot[cnt], number);
  1032. }
  1033. inode_sub_bytes(inode, number);
  1034. spin_unlock(&dq_data_lock);
  1035. /* Dirtify all the dquots - this can block when journalling */
  1036. for (cnt = 0; cnt < MAXQUOTAS; cnt++)
  1037. if (inode->i_dquot[cnt])
  1038. mark_dquot_dirty(inode->i_dquot[cnt]);
  1039. up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1040. return QUOTA_OK;
  1041. }
  1042. /*
  1043. * This operation can block, but only after everything is updated
  1044. */
  1045. int dquot_free_inode(const struct inode *inode, unsigned long number)
  1046. {
  1047. unsigned int cnt;
  1048. /* First test before acquiring mutex - solves deadlocks when we
  1049. * re-enter the quota code and are already holding the mutex */
  1050. if (IS_NOQUOTA(inode))
  1051. return QUOTA_OK;
  1052. down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1053. /* Now recheck reliably when holding dqptr_sem */
  1054. if (IS_NOQUOTA(inode)) {
  1055. up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1056. return QUOTA_OK;
  1057. }
  1058. spin_lock(&dq_data_lock);
  1059. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1060. if (inode->i_dquot[cnt] == NODQUOT)
  1061. continue;
  1062. dquot_decr_inodes(inode->i_dquot[cnt], number);
  1063. }
  1064. spin_unlock(&dq_data_lock);
  1065. /* Dirtify all the dquots - this can block when journalling */
  1066. for (cnt = 0; cnt < MAXQUOTAS; cnt++)
  1067. if (inode->i_dquot[cnt])
  1068. mark_dquot_dirty(inode->i_dquot[cnt]);
  1069. up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1070. return QUOTA_OK;
  1071. }
  1072. /*
  1073. * Transfer the number of inode and blocks from one diskquota to an other.
  1074. *
  1075. * This operation can block, but only after everything is updated
  1076. * A transaction must be started when entering this function.
  1077. */
  1078. int dquot_transfer(struct inode *inode, struct iattr *iattr)
  1079. {
  1080. qsize_t space;
  1081. struct dquot *transfer_from[MAXQUOTAS];
  1082. struct dquot *transfer_to[MAXQUOTAS];
  1083. int cnt, ret = NO_QUOTA, chuid = (iattr->ia_valid & ATTR_UID) && inode->i_uid != iattr->ia_uid,
  1084. chgid = (iattr->ia_valid & ATTR_GID) && inode->i_gid != iattr->ia_gid;
  1085. char warntype[MAXQUOTAS];
  1086. /* First test before acquiring mutex - solves deadlocks when we
  1087. * re-enter the quota code and are already holding the mutex */
  1088. if (IS_NOQUOTA(inode))
  1089. return QUOTA_OK;
  1090. /* Clear the arrays */
  1091. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1092. transfer_to[cnt] = transfer_from[cnt] = NODQUOT;
  1093. warntype[cnt] = NOWARN;
  1094. }
  1095. down_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1096. /* Now recheck reliably when holding dqptr_sem */
  1097. if (IS_NOQUOTA(inode)) { /* File without quota accounting? */
  1098. up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1099. return QUOTA_OK;
  1100. }
  1101. /* First build the transfer_to list - here we can block on
  1102. * reading/instantiating of dquots. We know that the transaction for
  1103. * us was already started so we don't violate lock ranking here */
  1104. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1105. switch (cnt) {
  1106. case USRQUOTA:
  1107. if (!chuid)
  1108. continue;
  1109. transfer_to[cnt] = dqget(inode->i_sb, iattr->ia_uid, cnt);
  1110. break;
  1111. case GRPQUOTA:
  1112. if (!chgid)
  1113. continue;
  1114. transfer_to[cnt] = dqget(inode->i_sb, iattr->ia_gid, cnt);
  1115. break;
  1116. }
  1117. }
  1118. spin_lock(&dq_data_lock);
  1119. space = inode_get_bytes(inode);
  1120. /* Build the transfer_from list and check the limits */
  1121. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1122. if (transfer_to[cnt] == NODQUOT)
  1123. continue;
  1124. transfer_from[cnt] = inode->i_dquot[cnt];
  1125. if (check_idq(transfer_to[cnt], 1, warntype+cnt) == NO_QUOTA ||
  1126. check_bdq(transfer_to[cnt], space, 0, warntype+cnt) == NO_QUOTA)
  1127. goto warn_put_all;
  1128. }
  1129. /*
  1130. * Finally perform the needed transfer from transfer_from to transfer_to
  1131. */
  1132. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1133. /*
  1134. * Skip changes for same uid or gid or for turned off quota-type.
  1135. */
  1136. if (transfer_to[cnt] == NODQUOT)
  1137. continue;
  1138. /* Due to IO error we might not have transfer_from[] structure */
  1139. if (transfer_from[cnt]) {
  1140. dquot_decr_inodes(transfer_from[cnt], 1);
  1141. dquot_decr_space(transfer_from[cnt], space);
  1142. }
  1143. dquot_incr_inodes(transfer_to[cnt], 1);
  1144. dquot_incr_space(transfer_to[cnt], space);
  1145. inode->i_dquot[cnt] = transfer_to[cnt];
  1146. }
  1147. ret = QUOTA_OK;
  1148. warn_put_all:
  1149. spin_unlock(&dq_data_lock);
  1150. /* Dirtify all the dquots - this can block when journalling */
  1151. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1152. if (transfer_from[cnt])
  1153. mark_dquot_dirty(transfer_from[cnt]);
  1154. if (transfer_to[cnt])
  1155. mark_dquot_dirty(transfer_to[cnt]);
  1156. }
  1157. flush_warnings(transfer_to, warntype);
  1158. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1159. if (ret == QUOTA_OK && transfer_from[cnt] != NODQUOT)
  1160. dqput(transfer_from[cnt]);
  1161. if (ret == NO_QUOTA && transfer_to[cnt] != NODQUOT)
  1162. dqput(transfer_to[cnt]);
  1163. }
  1164. up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1165. return ret;
  1166. }
  1167. /*
  1168. * Write info of quota file to disk
  1169. */
  1170. int dquot_commit_info(struct super_block *sb, int type)
  1171. {
  1172. int ret;
  1173. struct quota_info *dqopt = sb_dqopt(sb);
  1174. mutex_lock(&dqopt->dqio_mutex);
  1175. ret = dqopt->ops[type]->write_file_info(sb, type);
  1176. mutex_unlock(&dqopt->dqio_mutex);
  1177. return ret;
  1178. }
  1179. /*
  1180. * Definitions of diskquota operations.
  1181. */
  1182. struct dquot_operations dquot_operations = {
  1183. .initialize = dquot_initialize,
  1184. .drop = dquot_drop,
  1185. .alloc_space = dquot_alloc_space,
  1186. .alloc_inode = dquot_alloc_inode,
  1187. .free_space = dquot_free_space,
  1188. .free_inode = dquot_free_inode,
  1189. .transfer = dquot_transfer,
  1190. .write_dquot = dquot_commit,
  1191. .acquire_dquot = dquot_acquire,
  1192. .release_dquot = dquot_release,
  1193. .mark_dirty = dquot_mark_dquot_dirty,
  1194. .write_info = dquot_commit_info
  1195. };
  1196. static inline void set_enable_flags(struct quota_info *dqopt, int type)
  1197. {
  1198. switch (type) {
  1199. case USRQUOTA:
  1200. dqopt->flags |= DQUOT_USR_ENABLED;
  1201. break;
  1202. case GRPQUOTA:
  1203. dqopt->flags |= DQUOT_GRP_ENABLED;
  1204. break;
  1205. }
  1206. }
  1207. static inline void reset_enable_flags(struct quota_info *dqopt, int type)
  1208. {
  1209. switch (type) {
  1210. case USRQUOTA:
  1211. dqopt->flags &= ~DQUOT_USR_ENABLED;
  1212. break;
  1213. case GRPQUOTA:
  1214. dqopt->flags &= ~DQUOT_GRP_ENABLED;
  1215. break;
  1216. }
  1217. }
  1218. /*
  1219. * Turn quota off on a device. type == -1 ==> quotaoff for all types (umount)
  1220. */
  1221. int vfs_quota_off(struct super_block *sb, int type)
  1222. {
  1223. int cnt;
  1224. struct quota_info *dqopt = sb_dqopt(sb);
  1225. struct inode *toputinode[MAXQUOTAS];
  1226. /* We need to serialize quota_off() for device */
  1227. mutex_lock(&dqopt->dqonoff_mutex);
  1228. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1229. toputinode[cnt] = NULL;
  1230. if (type != -1 && cnt != type)
  1231. continue;
  1232. if (!sb_has_quota_enabled(sb, cnt))
  1233. continue;
  1234. reset_enable_flags(dqopt, cnt);
  1235. /* Note: these are blocking operations */
  1236. drop_dquot_ref(sb, cnt);
  1237. invalidate_dquots(sb, cnt);
  1238. /*
  1239. * Now all dquots should be invalidated, all writes done so we should be only
  1240. * users of the info. No locks needed.
  1241. */
  1242. if (info_dirty(&dqopt->info[cnt]))
  1243. sb->dq_op->write_info(sb, cnt);
  1244. if (dqopt->ops[cnt]->free_file_info)
  1245. dqopt->ops[cnt]->free_file_info(sb, cnt);
  1246. put_quota_format(dqopt->info[cnt].dqi_format);
  1247. toputinode[cnt] = dqopt->files[cnt];
  1248. dqopt->files[cnt] = NULL;
  1249. dqopt->info[cnt].dqi_flags = 0;
  1250. dqopt->info[cnt].dqi_igrace = 0;
  1251. dqopt->info[cnt].dqi_bgrace = 0;
  1252. dqopt->ops[cnt] = NULL;
  1253. }
  1254. mutex_unlock(&dqopt->dqonoff_mutex);
  1255. /* Sync the superblock so that buffers with quota data are written to
  1256. * disk (and so userspace sees correct data afterwards). */
  1257. if (sb->s_op->sync_fs)
  1258. sb->s_op->sync_fs(sb, 1);
  1259. sync_blockdev(sb->s_bdev);
  1260. /* Now the quota files are just ordinary files and we can set the
  1261. * inode flags back. Moreover we discard the pagecache so that
  1262. * userspace sees the writes we did bypassing the pagecache. We
  1263. * must also discard the blockdev buffers so that we see the
  1264. * changes done by userspace on the next quotaon() */
  1265. for (cnt = 0; cnt < MAXQUOTAS; cnt++)
  1266. if (toputinode[cnt]) {
  1267. mutex_lock(&dqopt->dqonoff_mutex);
  1268. /* If quota was reenabled in the meantime, we have
  1269. * nothing to do */
  1270. if (!sb_has_quota_enabled(sb, cnt)) {
  1271. mutex_lock(&toputinode[cnt]->i_mutex);
  1272. toputinode[cnt]->i_flags &= ~(S_IMMUTABLE |
  1273. S_NOATIME | S_NOQUOTA);
  1274. truncate_inode_pages(&toputinode[cnt]->i_data, 0);
  1275. mutex_unlock(&toputinode[cnt]->i_mutex);
  1276. mark_inode_dirty(toputinode[cnt]);
  1277. iput(toputinode[cnt]);
  1278. }
  1279. mutex_unlock(&dqopt->dqonoff_mutex);
  1280. }
  1281. if (sb->s_bdev)
  1282. invalidate_bdev(sb->s_bdev, 0);
  1283. return 0;
  1284. }
  1285. /*
  1286. * Turn quotas on on a device
  1287. */
  1288. /* Helper function when we already have the inode */
  1289. static int vfs_quota_on_inode(struct inode *inode, int type, int format_id)
  1290. {
  1291. struct quota_format_type *fmt = find_quota_format(format_id);
  1292. struct super_block *sb = inode->i_sb;
  1293. struct quota_info *dqopt = sb_dqopt(sb);
  1294. int error;
  1295. int oldflags = -1;
  1296. if (!fmt)
  1297. return -ESRCH;
  1298. if (!S_ISREG(inode->i_mode)) {
  1299. error = -EACCES;
  1300. goto out_fmt;
  1301. }
  1302. if (IS_RDONLY(inode)) {
  1303. error = -EROFS;
  1304. goto out_fmt;
  1305. }
  1306. if (!sb->s_op->quota_write || !sb->s_op->quota_read) {
  1307. error = -EINVAL;
  1308. goto out_fmt;
  1309. }
  1310. /* As we bypass the pagecache we must now flush the inode so that
  1311. * we see all the changes from userspace... */
  1312. write_inode_now(inode, 1);
  1313. /* And now flush the block cache so that kernel sees the changes */
  1314. invalidate_bdev(sb->s_bdev, 0);
  1315. mutex_lock(&inode->i_mutex);
  1316. mutex_lock(&dqopt->dqonoff_mutex);
  1317. if (sb_has_quota_enabled(sb, type)) {
  1318. error = -EBUSY;
  1319. goto out_lock;
  1320. }
  1321. /* We don't want quota and atime on quota files (deadlocks possible)
  1322. * Also nobody should write to the file - we use special IO operations
  1323. * which ignore the immutable bit. */
  1324. down_write(&dqopt->dqptr_sem);
  1325. oldflags = inode->i_flags & (S_NOATIME | S_IMMUTABLE | S_NOQUOTA);
  1326. inode->i_flags |= S_NOQUOTA | S_NOATIME | S_IMMUTABLE;
  1327. up_write(&dqopt->dqptr_sem);
  1328. sb->dq_op->drop(inode);
  1329. error = -EIO;
  1330. dqopt->files[type] = igrab(inode);
  1331. if (!dqopt->files[type])
  1332. goto out_lock;
  1333. error = -EINVAL;
  1334. if (!fmt->qf_ops->check_quota_file(sb, type))
  1335. goto out_file_init;
  1336. dqopt->ops[type] = fmt->qf_ops;
  1337. dqopt->info[type].dqi_format = fmt;
  1338. INIT_LIST_HEAD(&dqopt->info[type].dqi_dirty_list);
  1339. mutex_lock(&dqopt->dqio_mutex);
  1340. if ((error = dqopt->ops[type]->read_file_info(sb, type)) < 0) {
  1341. mutex_unlock(&dqopt->dqio_mutex);
  1342. goto out_file_init;
  1343. }
  1344. mutex_unlock(&dqopt->dqio_mutex);
  1345. mutex_unlock(&inode->i_mutex);
  1346. set_enable_flags(dqopt, type);
  1347. add_dquot_ref(sb, type);
  1348. mutex_unlock(&dqopt->dqonoff_mutex);
  1349. return 0;
  1350. out_file_init:
  1351. dqopt->files[type] = NULL;
  1352. iput(inode);
  1353. out_lock:
  1354. mutex_unlock(&dqopt->dqonoff_mutex);
  1355. if (oldflags != -1) {
  1356. down_write(&dqopt->dqptr_sem);
  1357. /* Set the flags back (in the case of accidental quotaon()
  1358. * on a wrong file we don't want to mess up the flags) */
  1359. inode->i_flags &= ~(S_NOATIME | S_NOQUOTA | S_IMMUTABLE);
  1360. inode->i_flags |= oldflags;
  1361. up_write(&dqopt->dqptr_sem);
  1362. }
  1363. mutex_unlock(&inode->i_mutex);
  1364. out_fmt:
  1365. put_quota_format(fmt);
  1366. return error;
  1367. }
  1368. /* Actual function called from quotactl() */
  1369. int vfs_quota_on(struct super_block *sb, int type, int format_id, char *path)
  1370. {
  1371. struct nameidata nd;
  1372. int error;
  1373. error = path_lookup(path, LOOKUP_FOLLOW, &nd);
  1374. if (error < 0)
  1375. return error;
  1376. error = security_quota_on(nd.dentry);
  1377. if (error)
  1378. goto out_path;
  1379. /* Quota file not on the same filesystem? */
  1380. if (nd.mnt->mnt_sb != sb)
  1381. error = -EXDEV;
  1382. else
  1383. error = vfs_quota_on_inode(nd.dentry->d_inode, type, format_id);
  1384. out_path:
  1385. path_release(&nd);
  1386. return error;
  1387. }
  1388. /*
  1389. * This function is used when filesystem needs to initialize quotas
  1390. * during mount time.
  1391. */
  1392. int vfs_quota_on_mount(struct super_block *sb, char *qf_name,
  1393. int format_id, int type)
  1394. {
  1395. struct dentry *dentry;
  1396. int error;
  1397. dentry = lookup_one_len(qf_name, sb->s_root, strlen(qf_name));
  1398. if (IS_ERR(dentry))
  1399. return PTR_ERR(dentry);
  1400. if (!dentry->d_inode) {
  1401. error = -ENOENT;
  1402. goto out;
  1403. }
  1404. error = security_quota_on(dentry);
  1405. if (!error)
  1406. error = vfs_quota_on_inode(dentry->d_inode, type, format_id);
  1407. out:
  1408. dput(dentry);
  1409. return error;
  1410. }
  1411. /* Generic routine for getting common part of quota structure */
  1412. static void do_get_dqblk(struct dquot *dquot, struct if_dqblk *di)
  1413. {
  1414. struct mem_dqblk *dm = &dquot->dq_dqb;
  1415. spin_lock(&dq_data_lock);
  1416. di->dqb_bhardlimit = dm->dqb_bhardlimit;
  1417. di->dqb_bsoftlimit = dm->dqb_bsoftlimit;
  1418. di->dqb_curspace = dm->dqb_curspace;
  1419. di->dqb_ihardlimit = dm->dqb_ihardlimit;
  1420. di->dqb_isoftlimit = dm->dqb_isoftlimit;
  1421. di->dqb_curinodes = dm->dqb_curinodes;
  1422. di->dqb_btime = dm->dqb_btime;
  1423. di->dqb_itime = dm->dqb_itime;
  1424. di->dqb_valid = QIF_ALL;
  1425. spin_unlock(&dq_data_lock);
  1426. }
  1427. int vfs_get_dqblk(struct super_block *sb, int type, qid_t id, struct if_dqblk *di)
  1428. {
  1429. struct dquot *dquot;
  1430. mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
  1431. if (!(dquot = dqget(sb, id, type))) {
  1432. mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
  1433. return -ESRCH;
  1434. }
  1435. do_get_dqblk(dquot, di);
  1436. dqput(dquot);
  1437. mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
  1438. return 0;
  1439. }
  1440. /* Generic routine for setting common part of quota structure */
  1441. static void do_set_dqblk(struct dquot *dquot, struct if_dqblk *di)
  1442. {
  1443. struct mem_dqblk *dm = &dquot->dq_dqb;
  1444. int check_blim = 0, check_ilim = 0;
  1445. spin_lock(&dq_data_lock);
  1446. if (di->dqb_valid & QIF_SPACE) {
  1447. dm->dqb_curspace = di->dqb_curspace;
  1448. check_blim = 1;
  1449. }
  1450. if (di->dqb_valid & QIF_BLIMITS) {
  1451. dm->dqb_bsoftlimit = di->dqb_bsoftlimit;
  1452. dm->dqb_bhardlimit = di->dqb_bhardlimit;
  1453. check_blim = 1;
  1454. }
  1455. if (di->dqb_valid & QIF_INODES) {
  1456. dm->dqb_curinodes = di->dqb_curinodes;
  1457. check_ilim = 1;
  1458. }
  1459. if (di->dqb_valid & QIF_ILIMITS) {
  1460. dm->dqb_isoftlimit = di->dqb_isoftlimit;
  1461. dm->dqb_ihardlimit = di->dqb_ihardlimit;
  1462. check_ilim = 1;
  1463. }
  1464. if (di->dqb_valid & QIF_BTIME)
  1465. dm->dqb_btime = di->dqb_btime;
  1466. if (di->dqb_valid & QIF_ITIME)
  1467. dm->dqb_itime = di->dqb_itime;
  1468. if (check_blim) {
  1469. if (!dm->dqb_bsoftlimit || toqb(dm->dqb_curspace) < dm->dqb_bsoftlimit) {
  1470. dm->dqb_btime = 0;
  1471. clear_bit(DQ_BLKS_B, &dquot->dq_flags);
  1472. }
  1473. else if (!(di->dqb_valid & QIF_BTIME)) /* Set grace only if user hasn't provided his own... */
  1474. dm->dqb_btime = get_seconds() + sb_dqopt(dquot->dq_sb)->info[dquot->dq_type].dqi_bgrace;
  1475. }
  1476. if (check_ilim) {
  1477. if (!dm->dqb_isoftlimit || dm->dqb_curinodes < dm->dqb_isoftlimit) {
  1478. dm->dqb_itime = 0;
  1479. clear_bit(DQ_INODES_B, &dquot->dq_flags);
  1480. }
  1481. else if (!(di->dqb_valid & QIF_ITIME)) /* Set grace only if user hasn't provided his own... */
  1482. dm->dqb_itime = get_seconds() + sb_dqopt(dquot->dq_sb)->info[dquot->dq_type].dqi_igrace;
  1483. }
  1484. if (dm->dqb_bhardlimit || dm->dqb_bsoftlimit || dm->dqb_ihardlimit || dm->dqb_isoftlimit)
  1485. clear_bit(DQ_FAKE_B, &dquot->dq_flags);
  1486. else
  1487. set_bit(DQ_FAKE_B, &dquot->dq_flags);
  1488. spin_unlock(&dq_data_lock);
  1489. mark_dquot_dirty(dquot);
  1490. }
  1491. int vfs_set_dqblk(struct super_block *sb, int type, qid_t id, struct if_dqblk *di)
  1492. {
  1493. struct dquot *dquot;
  1494. mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
  1495. if (!(dquot = dqget(sb, id, type))) {
  1496. mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
  1497. return -ESRCH;
  1498. }
  1499. do_set_dqblk(dquot, di);
  1500. dqput(dquot);
  1501. mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
  1502. return 0;
  1503. }
  1504. /* Generic routine for getting common part of quota file information */
  1505. int vfs_get_dqinfo(struct super_block *sb, int type, struct if_dqinfo *ii)
  1506. {
  1507. struct mem_dqinfo *mi;
  1508. mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
  1509. if (!sb_has_quota_enabled(sb, type)) {
  1510. mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
  1511. return -ESRCH;
  1512. }
  1513. mi = sb_dqopt(sb)->info + type;
  1514. spin_lock(&dq_data_lock);
  1515. ii->dqi_bgrace = mi->dqi_bgrace;
  1516. ii->dqi_igrace = mi->dqi_igrace;
  1517. ii->dqi_flags = mi->dqi_flags & DQF_MASK;
  1518. ii->dqi_valid = IIF_ALL;
  1519. spin_unlock(&dq_data_lock);
  1520. mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
  1521. return 0;
  1522. }
  1523. /* Generic routine for setting common part of quota file information */
  1524. int vfs_set_dqinfo(struct super_block *sb, int type, struct if_dqinfo *ii)
  1525. {
  1526. struct mem_dqinfo *mi;
  1527. mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
  1528. if (!sb_has_quota_enabled(sb, type)) {
  1529. mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
  1530. return -ESRCH;
  1531. }
  1532. mi = sb_dqopt(sb)->info + type;
  1533. spin_lock(&dq_data_lock);
  1534. if (ii->dqi_valid & IIF_BGRACE)
  1535. mi->dqi_bgrace = ii->dqi_bgrace;
  1536. if (ii->dqi_valid & IIF_IGRACE)
  1537. mi->dqi_igrace = ii->dqi_igrace;
  1538. if (ii->dqi_valid & IIF_FLAGS)
  1539. mi->dqi_flags = (mi->dqi_flags & ~DQF_MASK) | (ii->dqi_flags & DQF_MASK);
  1540. spin_unlock(&dq_data_lock);
  1541. mark_info_dirty(sb, type);
  1542. /* Force write to disk */
  1543. sb->dq_op->write_info(sb, type);
  1544. mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
  1545. return 0;
  1546. }
  1547. struct quotactl_ops vfs_quotactl_ops = {
  1548. .quota_on = vfs_quota_on,
  1549. .quota_off = vfs_quota_off,
  1550. .quota_sync = vfs_quota_sync,
  1551. .get_info = vfs_get_dqinfo,
  1552. .set_info = vfs_set_dqinfo,
  1553. .get_dqblk = vfs_get_dqblk,
  1554. .set_dqblk = vfs_set_dqblk
  1555. };
  1556. static ctl_table fs_dqstats_table[] = {
  1557. {
  1558. .ctl_name = FS_DQ_LOOKUPS,
  1559. .procname = "lookups",
  1560. .data = &dqstats.lookups,
  1561. .maxlen = sizeof(int),
  1562. .mode = 0444,
  1563. .proc_handler = &proc_dointvec,
  1564. },
  1565. {
  1566. .ctl_name = FS_DQ_DROPS,
  1567. .procname = "drops",
  1568. .data = &dqstats.drops,
  1569. .maxlen = sizeof(int),
  1570. .mode = 0444,
  1571. .proc_handler = &proc_dointvec,
  1572. },
  1573. {
  1574. .ctl_name = FS_DQ_READS,
  1575. .procname = "reads",
  1576. .data = &dqstats.reads,
  1577. .maxlen = sizeof(int),
  1578. .mode = 0444,
  1579. .proc_handler = &proc_dointvec,
  1580. },
  1581. {
  1582. .ctl_name = FS_DQ_WRITES,
  1583. .procname = "writes",
  1584. .data = &dqstats.writes,
  1585. .maxlen = sizeof(int),
  1586. .mode = 0444,
  1587. .proc_handler = &proc_dointvec,
  1588. },
  1589. {
  1590. .ctl_name = FS_DQ_CACHE_HITS,
  1591. .procname = "cache_hits",
  1592. .data = &dqstats.cache_hits,
  1593. .maxlen = sizeof(int),
  1594. .mode = 0444,
  1595. .proc_handler = &proc_dointvec,
  1596. },
  1597. {
  1598. .ctl_name = FS_DQ_ALLOCATED,
  1599. .procname = "allocated_dquots",
  1600. .data = &dqstats.allocated_dquots,
  1601. .maxlen = sizeof(int),
  1602. .mode = 0444,
  1603. .proc_handler = &proc_dointvec,
  1604. },
  1605. {
  1606. .ctl_name = FS_DQ_FREE,
  1607. .procname = "free_dquots",
  1608. .data = &dqstats.free_dquots,
  1609. .maxlen = sizeof(int),
  1610. .mode = 0444,
  1611. .proc_handler = &proc_dointvec,
  1612. },
  1613. {
  1614. .ctl_name = FS_DQ_SYNCS,
  1615. .procname = "syncs",
  1616. .data = &dqstats.syncs,
  1617. .maxlen = sizeof(int),
  1618. .mode = 0444,
  1619. .proc_handler = &proc_dointvec,
  1620. },
  1621. {
  1622. .ctl_name = FS_DQ_WARNINGS,
  1623. .procname = "warnings",
  1624. .data = &flag_print_warnings,
  1625. .maxlen = sizeof(int),
  1626. .mode = 0644,
  1627. .proc_handler = &proc_dointvec,
  1628. },
  1629. { .ctl_name = 0 },
  1630. };
  1631. static ctl_table fs_table[] = {
  1632. {
  1633. .ctl_name = FS_DQSTATS,
  1634. .procname = "quota",
  1635. .mode = 0555,
  1636. .child = fs_dqstats_table,
  1637. },
  1638. { .ctl_name = 0 },
  1639. };
  1640. static ctl_table sys_table[] = {
  1641. {
  1642. .ctl_name = CTL_FS,
  1643. .procname = "fs",
  1644. .mode = 0555,
  1645. .child = fs_table,
  1646. },
  1647. { .ctl_name = 0 },
  1648. };
  1649. static int __init dquot_init(void)
  1650. {
  1651. int i;
  1652. unsigned long nr_hash, order;
  1653. printk(KERN_NOTICE "VFS: Disk quotas %s\n", __DQUOT_VERSION__);
  1654. register_sysctl_table(sys_table, 0);
  1655. dquot_cachep = kmem_cache_create("dquot",
  1656. sizeof(struct dquot), sizeof(unsigned long) * 4,
  1657. (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
  1658. SLAB_MEM_SPREAD|SLAB_PANIC),
  1659. NULL, NULL);
  1660. order = 0;
  1661. dquot_hash = (struct hlist_head *)__get_free_pages(GFP_ATOMIC, order);
  1662. if (!dquot_hash)
  1663. panic("Cannot create dquot hash table");
  1664. /* Find power-of-two hlist_heads which can fit into allocation */
  1665. nr_hash = (1UL << order) * PAGE_SIZE / sizeof(struct hlist_head);
  1666. dq_hash_bits = 0;
  1667. do {
  1668. dq_hash_bits++;
  1669. } while (nr_hash >> dq_hash_bits);
  1670. dq_hash_bits--;
  1671. nr_hash = 1UL << dq_hash_bits;
  1672. dq_hash_mask = nr_hash - 1;
  1673. for (i = 0; i < nr_hash; i++)
  1674. INIT_HLIST_HEAD(dquot_hash + i);
  1675. printk("Dquot-cache hash table entries: %ld (order %ld, %ld bytes)\n",
  1676. nr_hash, order, (PAGE_SIZE << order));
  1677. set_shrinker(DEFAULT_SEEKS, shrink_dqcache_memory);
  1678. return 0;
  1679. }
  1680. module_init(dquot_init);
  1681. EXPORT_SYMBOL(register_quota_format);
  1682. EXPORT_SYMBOL(unregister_quota_format);
  1683. EXPORT_SYMBOL(dqstats);
  1684. EXPORT_SYMBOL(dq_data_lock);
  1685. EXPORT_SYMBOL(vfs_quota_on);
  1686. EXPORT_SYMBOL(vfs_quota_on_mount);
  1687. EXPORT_SYMBOL(vfs_quota_off);
  1688. EXPORT_SYMBOL(vfs_quota_sync);
  1689. EXPORT_SYMBOL(vfs_get_dqinfo);
  1690. EXPORT_SYMBOL(vfs_set_dqinfo);
  1691. EXPORT_SYMBOL(vfs_get_dqblk);
  1692. EXPORT_SYMBOL(vfs_set_dqblk);
  1693. EXPORT_SYMBOL(dquot_commit);
  1694. EXPORT_SYMBOL(dquot_commit_info);
  1695. EXPORT_SYMBOL(dquot_acquire);
  1696. EXPORT_SYMBOL(dquot_release);
  1697. EXPORT_SYMBOL(dquot_mark_dquot_dirty);
  1698. EXPORT_SYMBOL(dquot_initialize);
  1699. EXPORT_SYMBOL(dquot_drop);
  1700. EXPORT_SYMBOL(dquot_alloc_space);
  1701. EXPORT_SYMBOL(dquot_alloc_inode);
  1702. EXPORT_SYMBOL(dquot_free_space);
  1703. EXPORT_SYMBOL(dquot_free_inode);
  1704. EXPORT_SYMBOL(dquot_transfer);