dquot.c 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881
  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_tail(&dquot->dq_free, &free_dquots);
  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_tail(&dquot->dq_inuse, &inuse_list);
  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. BUG_ON(!list_empty(&dquot->dq_free));
  539. #endif
  540. put_dquot_last(dquot);
  541. spin_unlock(&dq_list_lock);
  542. }
  543. static struct dquot *get_empty_dquot(struct super_block *sb, int type)
  544. {
  545. struct dquot *dquot;
  546. dquot = kmem_cache_alloc(dquot_cachep, SLAB_NOFS);
  547. if(!dquot)
  548. return NODQUOT;
  549. memset((caddr_t)dquot, 0, sizeof(struct dquot));
  550. mutex_init(&dquot->dq_lock);
  551. INIT_LIST_HEAD(&dquot->dq_free);
  552. INIT_LIST_HEAD(&dquot->dq_inuse);
  553. INIT_HLIST_NODE(&dquot->dq_hash);
  554. INIT_LIST_HEAD(&dquot->dq_dirty);
  555. init_waitqueue_head(&dquot->dq_wait_unused);
  556. dquot->dq_sb = sb;
  557. dquot->dq_type = type;
  558. atomic_set(&dquot->dq_count, 1);
  559. return dquot;
  560. }
  561. /*
  562. * Get reference to dquot
  563. * MUST be called with either dqptr_sem or dqonoff_mutex held
  564. */
  565. static struct dquot *dqget(struct super_block *sb, unsigned int id, int type)
  566. {
  567. unsigned int hashent = hashfn(sb, id, type);
  568. struct dquot *dquot, *empty = NODQUOT;
  569. if (!sb_has_quota_enabled(sb, type))
  570. return NODQUOT;
  571. we_slept:
  572. spin_lock(&dq_list_lock);
  573. if ((dquot = find_dquot(hashent, sb, id, type)) == NODQUOT) {
  574. if (empty == NODQUOT) {
  575. spin_unlock(&dq_list_lock);
  576. if ((empty = get_empty_dquot(sb, type)) == NODQUOT)
  577. schedule(); /* Try to wait for a moment... */
  578. goto we_slept;
  579. }
  580. dquot = empty;
  581. dquot->dq_id = id;
  582. /* all dquots go on the inuse_list */
  583. put_inuse(dquot);
  584. /* hash it first so it can be found */
  585. insert_dquot_hash(dquot);
  586. dqstats.lookups++;
  587. spin_unlock(&dq_list_lock);
  588. } else {
  589. if (!atomic_read(&dquot->dq_count))
  590. remove_free_dquot(dquot);
  591. atomic_inc(&dquot->dq_count);
  592. dqstats.cache_hits++;
  593. dqstats.lookups++;
  594. spin_unlock(&dq_list_lock);
  595. if (empty)
  596. kmem_cache_free(dquot_cachep, empty);
  597. }
  598. /* Wait for dq_lock - after this we know that either dquot_release() is already
  599. * finished or it will be canceled due to dq_count > 1 test */
  600. wait_on_dquot(dquot);
  601. /* Read the dquot and instantiate it (everything done only if needed) */
  602. if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags) && sb->dq_op->acquire_dquot(dquot) < 0) {
  603. dqput(dquot);
  604. return NODQUOT;
  605. }
  606. #ifdef __DQUOT_PARANOIA
  607. BUG_ON(!dquot->dq_sb); /* Has somebody invalidated entry under us? */
  608. #endif
  609. return dquot;
  610. }
  611. static int dqinit_needed(struct inode *inode, int type)
  612. {
  613. int cnt;
  614. if (IS_NOQUOTA(inode))
  615. return 0;
  616. if (type != -1)
  617. return inode->i_dquot[type] == NODQUOT;
  618. for (cnt = 0; cnt < MAXQUOTAS; cnt++)
  619. if (inode->i_dquot[cnt] == NODQUOT)
  620. return 1;
  621. return 0;
  622. }
  623. /* This routine is guarded by dqonoff_mutex mutex */
  624. static void add_dquot_ref(struct super_block *sb, int type)
  625. {
  626. struct list_head *p;
  627. restart:
  628. file_list_lock();
  629. list_for_each(p, &sb->s_files) {
  630. struct file *filp = list_entry(p, struct file, f_u.fu_list);
  631. struct inode *inode = filp->f_dentry->d_inode;
  632. if (filp->f_mode & FMODE_WRITE && dqinit_needed(inode, type)) {
  633. struct dentry *dentry = dget(filp->f_dentry);
  634. file_list_unlock();
  635. sb->dq_op->initialize(inode, type);
  636. dput(dentry);
  637. /* As we may have blocked we had better restart... */
  638. goto restart;
  639. }
  640. }
  641. file_list_unlock();
  642. }
  643. /* Return 0 if dqput() won't block (note that 1 doesn't necessarily mean blocking) */
  644. static inline int dqput_blocks(struct dquot *dquot)
  645. {
  646. if (atomic_read(&dquot->dq_count) <= 1)
  647. return 1;
  648. return 0;
  649. }
  650. /* Remove references to dquots from inode - add dquot to list for freeing if needed */
  651. /* We can't race with anybody because we hold dqptr_sem for writing... */
  652. int remove_inode_dquot_ref(struct inode *inode, int type, struct list_head *tofree_head)
  653. {
  654. struct dquot *dquot = inode->i_dquot[type];
  655. inode->i_dquot[type] = NODQUOT;
  656. if (dquot != NODQUOT) {
  657. if (dqput_blocks(dquot)) {
  658. #ifdef __DQUOT_PARANOIA
  659. if (atomic_read(&dquot->dq_count) != 1)
  660. printk(KERN_WARNING "VFS: Adding dquot with dq_count %d to dispose list.\n", atomic_read(&dquot->dq_count));
  661. #endif
  662. spin_lock(&dq_list_lock);
  663. list_add(&dquot->dq_free, tofree_head); /* As dquot must have currently users it can't be on the free list... */
  664. spin_unlock(&dq_list_lock);
  665. return 1;
  666. }
  667. else
  668. dqput(dquot); /* We have guaranteed we won't block */
  669. }
  670. return 0;
  671. }
  672. /* Free list of dquots - called from inode.c */
  673. /* dquots are removed from inodes, no new references can be got so we are the only ones holding reference */
  674. static void put_dquot_list(struct list_head *tofree_head)
  675. {
  676. struct list_head *act_head;
  677. struct dquot *dquot;
  678. act_head = tofree_head->next;
  679. /* So now we have dquots on the list... Just free them */
  680. while (act_head != tofree_head) {
  681. dquot = list_entry(act_head, struct dquot, dq_free);
  682. act_head = act_head->next;
  683. list_del_init(&dquot->dq_free); /* Remove dquot from the list so we won't have problems... */
  684. dqput(dquot);
  685. }
  686. }
  687. /* Gather all references from inodes and drop them */
  688. static void drop_dquot_ref(struct super_block *sb, int type)
  689. {
  690. LIST_HEAD(tofree_head);
  691. down_write(&sb_dqopt(sb)->dqptr_sem);
  692. remove_dquot_ref(sb, type, &tofree_head);
  693. up_write(&sb_dqopt(sb)->dqptr_sem);
  694. put_dquot_list(&tofree_head);
  695. }
  696. static inline void dquot_incr_inodes(struct dquot *dquot, unsigned long number)
  697. {
  698. dquot->dq_dqb.dqb_curinodes += number;
  699. }
  700. static inline void dquot_incr_space(struct dquot *dquot, qsize_t number)
  701. {
  702. dquot->dq_dqb.dqb_curspace += number;
  703. }
  704. static inline void dquot_decr_inodes(struct dquot *dquot, unsigned long number)
  705. {
  706. if (dquot->dq_dqb.dqb_curinodes > number)
  707. dquot->dq_dqb.dqb_curinodes -= number;
  708. else
  709. dquot->dq_dqb.dqb_curinodes = 0;
  710. if (dquot->dq_dqb.dqb_curinodes <= dquot->dq_dqb.dqb_isoftlimit)
  711. dquot->dq_dqb.dqb_itime = (time_t) 0;
  712. clear_bit(DQ_INODES_B, &dquot->dq_flags);
  713. }
  714. static inline void dquot_decr_space(struct dquot *dquot, qsize_t number)
  715. {
  716. if (dquot->dq_dqb.dqb_curspace > number)
  717. dquot->dq_dqb.dqb_curspace -= number;
  718. else
  719. dquot->dq_dqb.dqb_curspace = 0;
  720. if (toqb(dquot->dq_dqb.dqb_curspace) <= dquot->dq_dqb.dqb_bsoftlimit)
  721. dquot->dq_dqb.dqb_btime = (time_t) 0;
  722. clear_bit(DQ_BLKS_B, &dquot->dq_flags);
  723. }
  724. static int flag_print_warnings = 1;
  725. static inline int need_print_warning(struct dquot *dquot)
  726. {
  727. if (!flag_print_warnings)
  728. return 0;
  729. switch (dquot->dq_type) {
  730. case USRQUOTA:
  731. return current->fsuid == dquot->dq_id;
  732. case GRPQUOTA:
  733. return in_group_p(dquot->dq_id);
  734. }
  735. return 0;
  736. }
  737. /* Values of warnings */
  738. #define NOWARN 0
  739. #define IHARDWARN 1
  740. #define ISOFTLONGWARN 2
  741. #define ISOFTWARN 3
  742. #define BHARDWARN 4
  743. #define BSOFTLONGWARN 5
  744. #define BSOFTWARN 6
  745. /* Print warning to user which exceeded quota */
  746. static void print_warning(struct dquot *dquot, const char warntype)
  747. {
  748. char *msg = NULL;
  749. int flag = (warntype == BHARDWARN || warntype == BSOFTLONGWARN) ? DQ_BLKS_B :
  750. ((warntype == IHARDWARN || warntype == ISOFTLONGWARN) ? DQ_INODES_B : 0);
  751. if (!need_print_warning(dquot) || (flag && test_and_set_bit(flag, &dquot->dq_flags)))
  752. return;
  753. mutex_lock(&tty_mutex);
  754. if (!current->signal->tty)
  755. goto out_lock;
  756. tty_write_message(current->signal->tty, dquot->dq_sb->s_id);
  757. if (warntype == ISOFTWARN || warntype == BSOFTWARN)
  758. tty_write_message(current->signal->tty, ": warning, ");
  759. else
  760. tty_write_message(current->signal->tty, ": write failed, ");
  761. tty_write_message(current->signal->tty, quotatypes[dquot->dq_type]);
  762. switch (warntype) {
  763. case IHARDWARN:
  764. msg = " file limit reached.\r\n";
  765. break;
  766. case ISOFTLONGWARN:
  767. msg = " file quota exceeded too long.\r\n";
  768. break;
  769. case ISOFTWARN:
  770. msg = " file quota exceeded.\r\n";
  771. break;
  772. case BHARDWARN:
  773. msg = " block limit reached.\r\n";
  774. break;
  775. case BSOFTLONGWARN:
  776. msg = " block quota exceeded too long.\r\n";
  777. break;
  778. case BSOFTWARN:
  779. msg = " block quota exceeded.\r\n";
  780. break;
  781. }
  782. tty_write_message(current->signal->tty, msg);
  783. out_lock:
  784. mutex_unlock(&tty_mutex);
  785. }
  786. static inline void flush_warnings(struct dquot **dquots, char *warntype)
  787. {
  788. int i;
  789. for (i = 0; i < MAXQUOTAS; i++)
  790. if (dquots[i] != NODQUOT && warntype[i] != NOWARN)
  791. print_warning(dquots[i], warntype[i]);
  792. }
  793. static inline char ignore_hardlimit(struct dquot *dquot)
  794. {
  795. struct mem_dqinfo *info = &sb_dqopt(dquot->dq_sb)->info[dquot->dq_type];
  796. return capable(CAP_SYS_RESOURCE) &&
  797. (info->dqi_format->qf_fmt_id != QFMT_VFS_OLD || !(info->dqi_flags & V1_DQF_RSQUASH));
  798. }
  799. /* needs dq_data_lock */
  800. static int check_idq(struct dquot *dquot, ulong inodes, char *warntype)
  801. {
  802. *warntype = NOWARN;
  803. if (inodes <= 0 || test_bit(DQ_FAKE_B, &dquot->dq_flags))
  804. return QUOTA_OK;
  805. if (dquot->dq_dqb.dqb_ihardlimit &&
  806. (dquot->dq_dqb.dqb_curinodes + inodes) > dquot->dq_dqb.dqb_ihardlimit &&
  807. !ignore_hardlimit(dquot)) {
  808. *warntype = IHARDWARN;
  809. return NO_QUOTA;
  810. }
  811. if (dquot->dq_dqb.dqb_isoftlimit &&
  812. (dquot->dq_dqb.dqb_curinodes + inodes) > dquot->dq_dqb.dqb_isoftlimit &&
  813. dquot->dq_dqb.dqb_itime && get_seconds() >= dquot->dq_dqb.dqb_itime &&
  814. !ignore_hardlimit(dquot)) {
  815. *warntype = ISOFTLONGWARN;
  816. return NO_QUOTA;
  817. }
  818. if (dquot->dq_dqb.dqb_isoftlimit &&
  819. (dquot->dq_dqb.dqb_curinodes + inodes) > dquot->dq_dqb.dqb_isoftlimit &&
  820. dquot->dq_dqb.dqb_itime == 0) {
  821. *warntype = ISOFTWARN;
  822. dquot->dq_dqb.dqb_itime = get_seconds() + sb_dqopt(dquot->dq_sb)->info[dquot->dq_type].dqi_igrace;
  823. }
  824. return QUOTA_OK;
  825. }
  826. /* needs dq_data_lock */
  827. static int check_bdq(struct dquot *dquot, qsize_t space, int prealloc, char *warntype)
  828. {
  829. *warntype = 0;
  830. if (space <= 0 || test_bit(DQ_FAKE_B, &dquot->dq_flags))
  831. return QUOTA_OK;
  832. if (dquot->dq_dqb.dqb_bhardlimit &&
  833. toqb(dquot->dq_dqb.dqb_curspace + space) > dquot->dq_dqb.dqb_bhardlimit &&
  834. !ignore_hardlimit(dquot)) {
  835. if (!prealloc)
  836. *warntype = BHARDWARN;
  837. return NO_QUOTA;
  838. }
  839. if (dquot->dq_dqb.dqb_bsoftlimit &&
  840. toqb(dquot->dq_dqb.dqb_curspace + space) > dquot->dq_dqb.dqb_bsoftlimit &&
  841. dquot->dq_dqb.dqb_btime && get_seconds() >= dquot->dq_dqb.dqb_btime &&
  842. !ignore_hardlimit(dquot)) {
  843. if (!prealloc)
  844. *warntype = BSOFTLONGWARN;
  845. return NO_QUOTA;
  846. }
  847. if (dquot->dq_dqb.dqb_bsoftlimit &&
  848. toqb(dquot->dq_dqb.dqb_curspace + space) > dquot->dq_dqb.dqb_bsoftlimit &&
  849. dquot->dq_dqb.dqb_btime == 0) {
  850. if (!prealloc) {
  851. *warntype = BSOFTWARN;
  852. dquot->dq_dqb.dqb_btime = get_seconds() + sb_dqopt(dquot->dq_sb)->info[dquot->dq_type].dqi_bgrace;
  853. }
  854. else
  855. /*
  856. * We don't allow preallocation to exceed softlimit so exceeding will
  857. * be always printed
  858. */
  859. return NO_QUOTA;
  860. }
  861. return QUOTA_OK;
  862. }
  863. /*
  864. * Initialize quota pointers in inode
  865. * Transaction must be started at entry
  866. */
  867. int dquot_initialize(struct inode *inode, int type)
  868. {
  869. unsigned int id = 0;
  870. int cnt, ret = 0;
  871. /* First test before acquiring mutex - solves deadlocks when we
  872. * re-enter the quota code and are already holding the mutex */
  873. if (IS_NOQUOTA(inode))
  874. return 0;
  875. down_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
  876. /* Having dqptr_sem we know NOQUOTA flags can't be altered... */
  877. if (IS_NOQUOTA(inode))
  878. goto out_err;
  879. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  880. if (type != -1 && cnt != type)
  881. continue;
  882. if (inode->i_dquot[cnt] == NODQUOT) {
  883. switch (cnt) {
  884. case USRQUOTA:
  885. id = inode->i_uid;
  886. break;
  887. case GRPQUOTA:
  888. id = inode->i_gid;
  889. break;
  890. }
  891. inode->i_dquot[cnt] = dqget(inode->i_sb, id, cnt);
  892. }
  893. }
  894. out_err:
  895. up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
  896. return ret;
  897. }
  898. /*
  899. * Release all quotas referenced by inode
  900. * Transaction must be started at an entry
  901. */
  902. int dquot_drop(struct inode *inode)
  903. {
  904. int cnt;
  905. down_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
  906. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  907. if (inode->i_dquot[cnt] != NODQUOT) {
  908. dqput(inode->i_dquot[cnt]);
  909. inode->i_dquot[cnt] = NODQUOT;
  910. }
  911. }
  912. up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
  913. return 0;
  914. }
  915. /*
  916. * Following four functions update i_blocks+i_bytes fields and
  917. * quota information (together with appropriate checks)
  918. * NOTE: We absolutely rely on the fact that caller dirties
  919. * the inode (usually macros in quotaops.h care about this) and
  920. * holds a handle for the current transaction so that dquot write and
  921. * inode write go into the same transaction.
  922. */
  923. /*
  924. * This operation can block, but only after everything is updated
  925. */
  926. int dquot_alloc_space(struct inode *inode, qsize_t number, int warn)
  927. {
  928. int cnt, ret = NO_QUOTA;
  929. char warntype[MAXQUOTAS];
  930. /* First test before acquiring mutex - solves deadlocks when we
  931. * re-enter the quota code and are already holding the mutex */
  932. if (IS_NOQUOTA(inode)) {
  933. out_add:
  934. inode_add_bytes(inode, number);
  935. return QUOTA_OK;
  936. }
  937. for (cnt = 0; cnt < MAXQUOTAS; cnt++)
  938. warntype[cnt] = NOWARN;
  939. down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  940. if (IS_NOQUOTA(inode)) { /* Now we can do reliable test... */
  941. up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  942. goto out_add;
  943. }
  944. spin_lock(&dq_data_lock);
  945. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  946. if (inode->i_dquot[cnt] == NODQUOT)
  947. continue;
  948. if (check_bdq(inode->i_dquot[cnt], number, warn, warntype+cnt) == NO_QUOTA)
  949. goto warn_put_all;
  950. }
  951. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  952. if (inode->i_dquot[cnt] == NODQUOT)
  953. continue;
  954. dquot_incr_space(inode->i_dquot[cnt], number);
  955. }
  956. inode_add_bytes(inode, number);
  957. ret = QUOTA_OK;
  958. warn_put_all:
  959. spin_unlock(&dq_data_lock);
  960. if (ret == QUOTA_OK)
  961. /* Dirtify all the dquots - this can block when journalling */
  962. for (cnt = 0; cnt < MAXQUOTAS; cnt++)
  963. if (inode->i_dquot[cnt])
  964. mark_dquot_dirty(inode->i_dquot[cnt]);
  965. flush_warnings(inode->i_dquot, warntype);
  966. up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  967. return ret;
  968. }
  969. /*
  970. * This operation can block, but only after everything is updated
  971. */
  972. int dquot_alloc_inode(const struct inode *inode, unsigned long number)
  973. {
  974. int cnt, ret = NO_QUOTA;
  975. char warntype[MAXQUOTAS];
  976. /* First test before acquiring mutex - solves deadlocks when we
  977. * re-enter the quota code and are already holding the mutex */
  978. if (IS_NOQUOTA(inode))
  979. return QUOTA_OK;
  980. for (cnt = 0; cnt < MAXQUOTAS; cnt++)
  981. warntype[cnt] = NOWARN;
  982. down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  983. if (IS_NOQUOTA(inode)) {
  984. up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  985. return QUOTA_OK;
  986. }
  987. spin_lock(&dq_data_lock);
  988. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  989. if (inode->i_dquot[cnt] == NODQUOT)
  990. continue;
  991. if (check_idq(inode->i_dquot[cnt], number, warntype+cnt) == NO_QUOTA)
  992. goto warn_put_all;
  993. }
  994. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  995. if (inode->i_dquot[cnt] == NODQUOT)
  996. continue;
  997. dquot_incr_inodes(inode->i_dquot[cnt], number);
  998. }
  999. ret = QUOTA_OK;
  1000. warn_put_all:
  1001. spin_unlock(&dq_data_lock);
  1002. if (ret == QUOTA_OK)
  1003. /* Dirtify all the dquots - this can block when journalling */
  1004. for (cnt = 0; cnt < MAXQUOTAS; cnt++)
  1005. if (inode->i_dquot[cnt])
  1006. mark_dquot_dirty(inode->i_dquot[cnt]);
  1007. flush_warnings((struct dquot **)inode->i_dquot, warntype);
  1008. up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1009. return ret;
  1010. }
  1011. /*
  1012. * This operation can block, but only after everything is updated
  1013. */
  1014. int dquot_free_space(struct inode *inode, qsize_t number)
  1015. {
  1016. unsigned int cnt;
  1017. /* First test before acquiring mutex - solves deadlocks when we
  1018. * re-enter the quota code and are already holding the mutex */
  1019. if (IS_NOQUOTA(inode)) {
  1020. out_sub:
  1021. inode_sub_bytes(inode, number);
  1022. return QUOTA_OK;
  1023. }
  1024. down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1025. /* Now recheck reliably when holding dqptr_sem */
  1026. if (IS_NOQUOTA(inode)) {
  1027. up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1028. goto out_sub;
  1029. }
  1030. spin_lock(&dq_data_lock);
  1031. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1032. if (inode->i_dquot[cnt] == NODQUOT)
  1033. continue;
  1034. dquot_decr_space(inode->i_dquot[cnt], number);
  1035. }
  1036. inode_sub_bytes(inode, number);
  1037. spin_unlock(&dq_data_lock);
  1038. /* Dirtify all the dquots - this can block when journalling */
  1039. for (cnt = 0; cnt < MAXQUOTAS; cnt++)
  1040. if (inode->i_dquot[cnt])
  1041. mark_dquot_dirty(inode->i_dquot[cnt]);
  1042. up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1043. return QUOTA_OK;
  1044. }
  1045. /*
  1046. * This operation can block, but only after everything is updated
  1047. */
  1048. int dquot_free_inode(const struct inode *inode, unsigned long number)
  1049. {
  1050. unsigned int cnt;
  1051. /* First test before acquiring mutex - solves deadlocks when we
  1052. * re-enter the quota code and are already holding the mutex */
  1053. if (IS_NOQUOTA(inode))
  1054. return QUOTA_OK;
  1055. down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1056. /* Now recheck reliably when holding dqptr_sem */
  1057. if (IS_NOQUOTA(inode)) {
  1058. up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1059. return QUOTA_OK;
  1060. }
  1061. spin_lock(&dq_data_lock);
  1062. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1063. if (inode->i_dquot[cnt] == NODQUOT)
  1064. continue;
  1065. dquot_decr_inodes(inode->i_dquot[cnt], number);
  1066. }
  1067. spin_unlock(&dq_data_lock);
  1068. /* Dirtify all the dquots - this can block when journalling */
  1069. for (cnt = 0; cnt < MAXQUOTAS; cnt++)
  1070. if (inode->i_dquot[cnt])
  1071. mark_dquot_dirty(inode->i_dquot[cnt]);
  1072. up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1073. return QUOTA_OK;
  1074. }
  1075. /*
  1076. * Transfer the number of inode and blocks from one diskquota to an other.
  1077. *
  1078. * This operation can block, but only after everything is updated
  1079. * A transaction must be started when entering this function.
  1080. */
  1081. int dquot_transfer(struct inode *inode, struct iattr *iattr)
  1082. {
  1083. qsize_t space;
  1084. struct dquot *transfer_from[MAXQUOTAS];
  1085. struct dquot *transfer_to[MAXQUOTAS];
  1086. int cnt, ret = NO_QUOTA, chuid = (iattr->ia_valid & ATTR_UID) && inode->i_uid != iattr->ia_uid,
  1087. chgid = (iattr->ia_valid & ATTR_GID) && inode->i_gid != iattr->ia_gid;
  1088. char warntype[MAXQUOTAS];
  1089. /* First test before acquiring mutex - solves deadlocks when we
  1090. * re-enter the quota code and are already holding the mutex */
  1091. if (IS_NOQUOTA(inode))
  1092. return QUOTA_OK;
  1093. /* Clear the arrays */
  1094. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1095. transfer_to[cnt] = transfer_from[cnt] = NODQUOT;
  1096. warntype[cnt] = NOWARN;
  1097. }
  1098. down_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1099. /* Now recheck reliably when holding dqptr_sem */
  1100. if (IS_NOQUOTA(inode)) { /* File without quota accounting? */
  1101. up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1102. return QUOTA_OK;
  1103. }
  1104. /* First build the transfer_to list - here we can block on
  1105. * reading/instantiating of dquots. We know that the transaction for
  1106. * us was already started so we don't violate lock ranking here */
  1107. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1108. switch (cnt) {
  1109. case USRQUOTA:
  1110. if (!chuid)
  1111. continue;
  1112. transfer_to[cnt] = dqget(inode->i_sb, iattr->ia_uid, cnt);
  1113. break;
  1114. case GRPQUOTA:
  1115. if (!chgid)
  1116. continue;
  1117. transfer_to[cnt] = dqget(inode->i_sb, iattr->ia_gid, cnt);
  1118. break;
  1119. }
  1120. }
  1121. spin_lock(&dq_data_lock);
  1122. space = inode_get_bytes(inode);
  1123. /* Build the transfer_from list and check the limits */
  1124. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1125. if (transfer_to[cnt] == NODQUOT)
  1126. continue;
  1127. transfer_from[cnt] = inode->i_dquot[cnt];
  1128. if (check_idq(transfer_to[cnt], 1, warntype+cnt) == NO_QUOTA ||
  1129. check_bdq(transfer_to[cnt], space, 0, warntype+cnt) == NO_QUOTA)
  1130. goto warn_put_all;
  1131. }
  1132. /*
  1133. * Finally perform the needed transfer from transfer_from to transfer_to
  1134. */
  1135. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1136. /*
  1137. * Skip changes for same uid or gid or for turned off quota-type.
  1138. */
  1139. if (transfer_to[cnt] == NODQUOT)
  1140. continue;
  1141. /* Due to IO error we might not have transfer_from[] structure */
  1142. if (transfer_from[cnt]) {
  1143. dquot_decr_inodes(transfer_from[cnt], 1);
  1144. dquot_decr_space(transfer_from[cnt], space);
  1145. }
  1146. dquot_incr_inodes(transfer_to[cnt], 1);
  1147. dquot_incr_space(transfer_to[cnt], space);
  1148. inode->i_dquot[cnt] = transfer_to[cnt];
  1149. }
  1150. ret = QUOTA_OK;
  1151. warn_put_all:
  1152. spin_unlock(&dq_data_lock);
  1153. /* Dirtify all the dquots - this can block when journalling */
  1154. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1155. if (transfer_from[cnt])
  1156. mark_dquot_dirty(transfer_from[cnt]);
  1157. if (transfer_to[cnt])
  1158. mark_dquot_dirty(transfer_to[cnt]);
  1159. }
  1160. flush_warnings(transfer_to, warntype);
  1161. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1162. if (ret == QUOTA_OK && transfer_from[cnt] != NODQUOT)
  1163. dqput(transfer_from[cnt]);
  1164. if (ret == NO_QUOTA && transfer_to[cnt] != NODQUOT)
  1165. dqput(transfer_to[cnt]);
  1166. }
  1167. up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1168. return ret;
  1169. }
  1170. /*
  1171. * Write info of quota file to disk
  1172. */
  1173. int dquot_commit_info(struct super_block *sb, int type)
  1174. {
  1175. int ret;
  1176. struct quota_info *dqopt = sb_dqopt(sb);
  1177. mutex_lock(&dqopt->dqio_mutex);
  1178. ret = dqopt->ops[type]->write_file_info(sb, type);
  1179. mutex_unlock(&dqopt->dqio_mutex);
  1180. return ret;
  1181. }
  1182. /*
  1183. * Definitions of diskquota operations.
  1184. */
  1185. struct dquot_operations dquot_operations = {
  1186. .initialize = dquot_initialize,
  1187. .drop = dquot_drop,
  1188. .alloc_space = dquot_alloc_space,
  1189. .alloc_inode = dquot_alloc_inode,
  1190. .free_space = dquot_free_space,
  1191. .free_inode = dquot_free_inode,
  1192. .transfer = dquot_transfer,
  1193. .write_dquot = dquot_commit,
  1194. .acquire_dquot = dquot_acquire,
  1195. .release_dquot = dquot_release,
  1196. .mark_dirty = dquot_mark_dquot_dirty,
  1197. .write_info = dquot_commit_info
  1198. };
  1199. static inline void set_enable_flags(struct quota_info *dqopt, int type)
  1200. {
  1201. switch (type) {
  1202. case USRQUOTA:
  1203. dqopt->flags |= DQUOT_USR_ENABLED;
  1204. break;
  1205. case GRPQUOTA:
  1206. dqopt->flags |= DQUOT_GRP_ENABLED;
  1207. break;
  1208. }
  1209. }
  1210. static inline void reset_enable_flags(struct quota_info *dqopt, int type)
  1211. {
  1212. switch (type) {
  1213. case USRQUOTA:
  1214. dqopt->flags &= ~DQUOT_USR_ENABLED;
  1215. break;
  1216. case GRPQUOTA:
  1217. dqopt->flags &= ~DQUOT_GRP_ENABLED;
  1218. break;
  1219. }
  1220. }
  1221. /*
  1222. * Turn quota off on a device. type == -1 ==> quotaoff for all types (umount)
  1223. */
  1224. int vfs_quota_off(struct super_block *sb, int type)
  1225. {
  1226. int cnt;
  1227. struct quota_info *dqopt = sb_dqopt(sb);
  1228. struct inode *toputinode[MAXQUOTAS];
  1229. /* We need to serialize quota_off() for device */
  1230. mutex_lock(&dqopt->dqonoff_mutex);
  1231. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1232. toputinode[cnt] = NULL;
  1233. if (type != -1 && cnt != type)
  1234. continue;
  1235. if (!sb_has_quota_enabled(sb, cnt))
  1236. continue;
  1237. reset_enable_flags(dqopt, cnt);
  1238. /* Note: these are blocking operations */
  1239. drop_dquot_ref(sb, cnt);
  1240. invalidate_dquots(sb, cnt);
  1241. /*
  1242. * Now all dquots should be invalidated, all writes done so we should be only
  1243. * users of the info. No locks needed.
  1244. */
  1245. if (info_dirty(&dqopt->info[cnt]))
  1246. sb->dq_op->write_info(sb, cnt);
  1247. if (dqopt->ops[cnt]->free_file_info)
  1248. dqopt->ops[cnt]->free_file_info(sb, cnt);
  1249. put_quota_format(dqopt->info[cnt].dqi_format);
  1250. toputinode[cnt] = dqopt->files[cnt];
  1251. dqopt->files[cnt] = NULL;
  1252. dqopt->info[cnt].dqi_flags = 0;
  1253. dqopt->info[cnt].dqi_igrace = 0;
  1254. dqopt->info[cnt].dqi_bgrace = 0;
  1255. dqopt->ops[cnt] = NULL;
  1256. }
  1257. mutex_unlock(&dqopt->dqonoff_mutex);
  1258. /* Sync the superblock so that buffers with quota data are written to
  1259. * disk (and so userspace sees correct data afterwards). */
  1260. if (sb->s_op->sync_fs)
  1261. sb->s_op->sync_fs(sb, 1);
  1262. sync_blockdev(sb->s_bdev);
  1263. /* Now the quota files are just ordinary files and we can set the
  1264. * inode flags back. Moreover we discard the pagecache so that
  1265. * userspace sees the writes we did bypassing the pagecache. We
  1266. * must also discard the blockdev buffers so that we see the
  1267. * changes done by userspace on the next quotaon() */
  1268. for (cnt = 0; cnt < MAXQUOTAS; cnt++)
  1269. if (toputinode[cnt]) {
  1270. mutex_lock(&dqopt->dqonoff_mutex);
  1271. /* If quota was reenabled in the meantime, we have
  1272. * nothing to do */
  1273. if (!sb_has_quota_enabled(sb, cnt)) {
  1274. mutex_lock(&toputinode[cnt]->i_mutex);
  1275. toputinode[cnt]->i_flags &= ~(S_IMMUTABLE |
  1276. S_NOATIME | S_NOQUOTA);
  1277. truncate_inode_pages(&toputinode[cnt]->i_data, 0);
  1278. mutex_unlock(&toputinode[cnt]->i_mutex);
  1279. mark_inode_dirty(toputinode[cnt]);
  1280. iput(toputinode[cnt]);
  1281. }
  1282. mutex_unlock(&dqopt->dqonoff_mutex);
  1283. }
  1284. if (sb->s_bdev)
  1285. invalidate_bdev(sb->s_bdev, 0);
  1286. return 0;
  1287. }
  1288. /*
  1289. * Turn quotas on on a device
  1290. */
  1291. /* Helper function when we already have the inode */
  1292. static int vfs_quota_on_inode(struct inode *inode, int type, int format_id)
  1293. {
  1294. struct quota_format_type *fmt = find_quota_format(format_id);
  1295. struct super_block *sb = inode->i_sb;
  1296. struct quota_info *dqopt = sb_dqopt(sb);
  1297. int error;
  1298. int oldflags = -1;
  1299. if (!fmt)
  1300. return -ESRCH;
  1301. if (!S_ISREG(inode->i_mode)) {
  1302. error = -EACCES;
  1303. goto out_fmt;
  1304. }
  1305. if (IS_RDONLY(inode)) {
  1306. error = -EROFS;
  1307. goto out_fmt;
  1308. }
  1309. if (!sb->s_op->quota_write || !sb->s_op->quota_read) {
  1310. error = -EINVAL;
  1311. goto out_fmt;
  1312. }
  1313. /* As we bypass the pagecache we must now flush the inode so that
  1314. * we see all the changes from userspace... */
  1315. write_inode_now(inode, 1);
  1316. /* And now flush the block cache so that kernel sees the changes */
  1317. invalidate_bdev(sb->s_bdev, 0);
  1318. mutex_lock(&inode->i_mutex);
  1319. mutex_lock(&dqopt->dqonoff_mutex);
  1320. if (sb_has_quota_enabled(sb, type)) {
  1321. error = -EBUSY;
  1322. goto out_lock;
  1323. }
  1324. /* We don't want quota and atime on quota files (deadlocks possible)
  1325. * Also nobody should write to the file - we use special IO operations
  1326. * which ignore the immutable bit. */
  1327. down_write(&dqopt->dqptr_sem);
  1328. oldflags = inode->i_flags & (S_NOATIME | S_IMMUTABLE | S_NOQUOTA);
  1329. inode->i_flags |= S_NOQUOTA | S_NOATIME | S_IMMUTABLE;
  1330. up_write(&dqopt->dqptr_sem);
  1331. sb->dq_op->drop(inode);
  1332. error = -EIO;
  1333. dqopt->files[type] = igrab(inode);
  1334. if (!dqopt->files[type])
  1335. goto out_lock;
  1336. error = -EINVAL;
  1337. if (!fmt->qf_ops->check_quota_file(sb, type))
  1338. goto out_file_init;
  1339. dqopt->ops[type] = fmt->qf_ops;
  1340. dqopt->info[type].dqi_format = fmt;
  1341. INIT_LIST_HEAD(&dqopt->info[type].dqi_dirty_list);
  1342. mutex_lock(&dqopt->dqio_mutex);
  1343. if ((error = dqopt->ops[type]->read_file_info(sb, type)) < 0) {
  1344. mutex_unlock(&dqopt->dqio_mutex);
  1345. goto out_file_init;
  1346. }
  1347. mutex_unlock(&dqopt->dqio_mutex);
  1348. mutex_unlock(&inode->i_mutex);
  1349. set_enable_flags(dqopt, type);
  1350. add_dquot_ref(sb, type);
  1351. mutex_unlock(&dqopt->dqonoff_mutex);
  1352. return 0;
  1353. out_file_init:
  1354. dqopt->files[type] = NULL;
  1355. iput(inode);
  1356. out_lock:
  1357. mutex_unlock(&dqopt->dqonoff_mutex);
  1358. if (oldflags != -1) {
  1359. down_write(&dqopt->dqptr_sem);
  1360. /* Set the flags back (in the case of accidental quotaon()
  1361. * on a wrong file we don't want to mess up the flags) */
  1362. inode->i_flags &= ~(S_NOATIME | S_NOQUOTA | S_IMMUTABLE);
  1363. inode->i_flags |= oldflags;
  1364. up_write(&dqopt->dqptr_sem);
  1365. }
  1366. mutex_unlock(&inode->i_mutex);
  1367. out_fmt:
  1368. put_quota_format(fmt);
  1369. return error;
  1370. }
  1371. /* Actual function called from quotactl() */
  1372. int vfs_quota_on(struct super_block *sb, int type, int format_id, char *path)
  1373. {
  1374. struct nameidata nd;
  1375. int error;
  1376. error = path_lookup(path, LOOKUP_FOLLOW, &nd);
  1377. if (error < 0)
  1378. return error;
  1379. error = security_quota_on(nd.dentry);
  1380. if (error)
  1381. goto out_path;
  1382. /* Quota file not on the same filesystem? */
  1383. if (nd.mnt->mnt_sb != sb)
  1384. error = -EXDEV;
  1385. else
  1386. error = vfs_quota_on_inode(nd.dentry->d_inode, type, format_id);
  1387. out_path:
  1388. path_release(&nd);
  1389. return error;
  1390. }
  1391. /*
  1392. * This function is used when filesystem needs to initialize quotas
  1393. * during mount time.
  1394. */
  1395. int vfs_quota_on_mount(struct super_block *sb, char *qf_name,
  1396. int format_id, int type)
  1397. {
  1398. struct dentry *dentry;
  1399. int error;
  1400. dentry = lookup_one_len(qf_name, sb->s_root, strlen(qf_name));
  1401. if (IS_ERR(dentry))
  1402. return PTR_ERR(dentry);
  1403. if (!dentry->d_inode) {
  1404. error = -ENOENT;
  1405. goto out;
  1406. }
  1407. error = security_quota_on(dentry);
  1408. if (!error)
  1409. error = vfs_quota_on_inode(dentry->d_inode, type, format_id);
  1410. out:
  1411. dput(dentry);
  1412. return error;
  1413. }
  1414. /* Generic routine for getting common part of quota structure */
  1415. static void do_get_dqblk(struct dquot *dquot, struct if_dqblk *di)
  1416. {
  1417. struct mem_dqblk *dm = &dquot->dq_dqb;
  1418. spin_lock(&dq_data_lock);
  1419. di->dqb_bhardlimit = dm->dqb_bhardlimit;
  1420. di->dqb_bsoftlimit = dm->dqb_bsoftlimit;
  1421. di->dqb_curspace = dm->dqb_curspace;
  1422. di->dqb_ihardlimit = dm->dqb_ihardlimit;
  1423. di->dqb_isoftlimit = dm->dqb_isoftlimit;
  1424. di->dqb_curinodes = dm->dqb_curinodes;
  1425. di->dqb_btime = dm->dqb_btime;
  1426. di->dqb_itime = dm->dqb_itime;
  1427. di->dqb_valid = QIF_ALL;
  1428. spin_unlock(&dq_data_lock);
  1429. }
  1430. int vfs_get_dqblk(struct super_block *sb, int type, qid_t id, struct if_dqblk *di)
  1431. {
  1432. struct dquot *dquot;
  1433. mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
  1434. if (!(dquot = dqget(sb, id, type))) {
  1435. mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
  1436. return -ESRCH;
  1437. }
  1438. do_get_dqblk(dquot, di);
  1439. dqput(dquot);
  1440. mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
  1441. return 0;
  1442. }
  1443. /* Generic routine for setting common part of quota structure */
  1444. static void do_set_dqblk(struct dquot *dquot, struct if_dqblk *di)
  1445. {
  1446. struct mem_dqblk *dm = &dquot->dq_dqb;
  1447. int check_blim = 0, check_ilim = 0;
  1448. spin_lock(&dq_data_lock);
  1449. if (di->dqb_valid & QIF_SPACE) {
  1450. dm->dqb_curspace = di->dqb_curspace;
  1451. check_blim = 1;
  1452. }
  1453. if (di->dqb_valid & QIF_BLIMITS) {
  1454. dm->dqb_bsoftlimit = di->dqb_bsoftlimit;
  1455. dm->dqb_bhardlimit = di->dqb_bhardlimit;
  1456. check_blim = 1;
  1457. }
  1458. if (di->dqb_valid & QIF_INODES) {
  1459. dm->dqb_curinodes = di->dqb_curinodes;
  1460. check_ilim = 1;
  1461. }
  1462. if (di->dqb_valid & QIF_ILIMITS) {
  1463. dm->dqb_isoftlimit = di->dqb_isoftlimit;
  1464. dm->dqb_ihardlimit = di->dqb_ihardlimit;
  1465. check_ilim = 1;
  1466. }
  1467. if (di->dqb_valid & QIF_BTIME)
  1468. dm->dqb_btime = di->dqb_btime;
  1469. if (di->dqb_valid & QIF_ITIME)
  1470. dm->dqb_itime = di->dqb_itime;
  1471. if (check_blim) {
  1472. if (!dm->dqb_bsoftlimit || toqb(dm->dqb_curspace) < dm->dqb_bsoftlimit) {
  1473. dm->dqb_btime = 0;
  1474. clear_bit(DQ_BLKS_B, &dquot->dq_flags);
  1475. }
  1476. else if (!(di->dqb_valid & QIF_BTIME)) /* Set grace only if user hasn't provided his own... */
  1477. dm->dqb_btime = get_seconds() + sb_dqopt(dquot->dq_sb)->info[dquot->dq_type].dqi_bgrace;
  1478. }
  1479. if (check_ilim) {
  1480. if (!dm->dqb_isoftlimit || dm->dqb_curinodes < dm->dqb_isoftlimit) {
  1481. dm->dqb_itime = 0;
  1482. clear_bit(DQ_INODES_B, &dquot->dq_flags);
  1483. }
  1484. else if (!(di->dqb_valid & QIF_ITIME)) /* Set grace only if user hasn't provided his own... */
  1485. dm->dqb_itime = get_seconds() + sb_dqopt(dquot->dq_sb)->info[dquot->dq_type].dqi_igrace;
  1486. }
  1487. if (dm->dqb_bhardlimit || dm->dqb_bsoftlimit || dm->dqb_ihardlimit || dm->dqb_isoftlimit)
  1488. clear_bit(DQ_FAKE_B, &dquot->dq_flags);
  1489. else
  1490. set_bit(DQ_FAKE_B, &dquot->dq_flags);
  1491. spin_unlock(&dq_data_lock);
  1492. mark_dquot_dirty(dquot);
  1493. }
  1494. int vfs_set_dqblk(struct super_block *sb, int type, qid_t id, struct if_dqblk *di)
  1495. {
  1496. struct dquot *dquot;
  1497. mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
  1498. if (!(dquot = dqget(sb, id, type))) {
  1499. mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
  1500. return -ESRCH;
  1501. }
  1502. do_set_dqblk(dquot, di);
  1503. dqput(dquot);
  1504. mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
  1505. return 0;
  1506. }
  1507. /* Generic routine for getting common part of quota file information */
  1508. int vfs_get_dqinfo(struct super_block *sb, int type, struct if_dqinfo *ii)
  1509. {
  1510. struct mem_dqinfo *mi;
  1511. mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
  1512. if (!sb_has_quota_enabled(sb, type)) {
  1513. mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
  1514. return -ESRCH;
  1515. }
  1516. mi = sb_dqopt(sb)->info + type;
  1517. spin_lock(&dq_data_lock);
  1518. ii->dqi_bgrace = mi->dqi_bgrace;
  1519. ii->dqi_igrace = mi->dqi_igrace;
  1520. ii->dqi_flags = mi->dqi_flags & DQF_MASK;
  1521. ii->dqi_valid = IIF_ALL;
  1522. spin_unlock(&dq_data_lock);
  1523. mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
  1524. return 0;
  1525. }
  1526. /* Generic routine for setting common part of quota file information */
  1527. int vfs_set_dqinfo(struct super_block *sb, int type, struct if_dqinfo *ii)
  1528. {
  1529. struct mem_dqinfo *mi;
  1530. mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
  1531. if (!sb_has_quota_enabled(sb, type)) {
  1532. mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
  1533. return -ESRCH;
  1534. }
  1535. mi = sb_dqopt(sb)->info + type;
  1536. spin_lock(&dq_data_lock);
  1537. if (ii->dqi_valid & IIF_BGRACE)
  1538. mi->dqi_bgrace = ii->dqi_bgrace;
  1539. if (ii->dqi_valid & IIF_IGRACE)
  1540. mi->dqi_igrace = ii->dqi_igrace;
  1541. if (ii->dqi_valid & IIF_FLAGS)
  1542. mi->dqi_flags = (mi->dqi_flags & ~DQF_MASK) | (ii->dqi_flags & DQF_MASK);
  1543. spin_unlock(&dq_data_lock);
  1544. mark_info_dirty(sb, type);
  1545. /* Force write to disk */
  1546. sb->dq_op->write_info(sb, type);
  1547. mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
  1548. return 0;
  1549. }
  1550. struct quotactl_ops vfs_quotactl_ops = {
  1551. .quota_on = vfs_quota_on,
  1552. .quota_off = vfs_quota_off,
  1553. .quota_sync = vfs_quota_sync,
  1554. .get_info = vfs_get_dqinfo,
  1555. .set_info = vfs_set_dqinfo,
  1556. .get_dqblk = vfs_get_dqblk,
  1557. .set_dqblk = vfs_set_dqblk
  1558. };
  1559. static ctl_table fs_dqstats_table[] = {
  1560. {
  1561. .ctl_name = FS_DQ_LOOKUPS,
  1562. .procname = "lookups",
  1563. .data = &dqstats.lookups,
  1564. .maxlen = sizeof(int),
  1565. .mode = 0444,
  1566. .proc_handler = &proc_dointvec,
  1567. },
  1568. {
  1569. .ctl_name = FS_DQ_DROPS,
  1570. .procname = "drops",
  1571. .data = &dqstats.drops,
  1572. .maxlen = sizeof(int),
  1573. .mode = 0444,
  1574. .proc_handler = &proc_dointvec,
  1575. },
  1576. {
  1577. .ctl_name = FS_DQ_READS,
  1578. .procname = "reads",
  1579. .data = &dqstats.reads,
  1580. .maxlen = sizeof(int),
  1581. .mode = 0444,
  1582. .proc_handler = &proc_dointvec,
  1583. },
  1584. {
  1585. .ctl_name = FS_DQ_WRITES,
  1586. .procname = "writes",
  1587. .data = &dqstats.writes,
  1588. .maxlen = sizeof(int),
  1589. .mode = 0444,
  1590. .proc_handler = &proc_dointvec,
  1591. },
  1592. {
  1593. .ctl_name = FS_DQ_CACHE_HITS,
  1594. .procname = "cache_hits",
  1595. .data = &dqstats.cache_hits,
  1596. .maxlen = sizeof(int),
  1597. .mode = 0444,
  1598. .proc_handler = &proc_dointvec,
  1599. },
  1600. {
  1601. .ctl_name = FS_DQ_ALLOCATED,
  1602. .procname = "allocated_dquots",
  1603. .data = &dqstats.allocated_dquots,
  1604. .maxlen = sizeof(int),
  1605. .mode = 0444,
  1606. .proc_handler = &proc_dointvec,
  1607. },
  1608. {
  1609. .ctl_name = FS_DQ_FREE,
  1610. .procname = "free_dquots",
  1611. .data = &dqstats.free_dquots,
  1612. .maxlen = sizeof(int),
  1613. .mode = 0444,
  1614. .proc_handler = &proc_dointvec,
  1615. },
  1616. {
  1617. .ctl_name = FS_DQ_SYNCS,
  1618. .procname = "syncs",
  1619. .data = &dqstats.syncs,
  1620. .maxlen = sizeof(int),
  1621. .mode = 0444,
  1622. .proc_handler = &proc_dointvec,
  1623. },
  1624. {
  1625. .ctl_name = FS_DQ_WARNINGS,
  1626. .procname = "warnings",
  1627. .data = &flag_print_warnings,
  1628. .maxlen = sizeof(int),
  1629. .mode = 0644,
  1630. .proc_handler = &proc_dointvec,
  1631. },
  1632. { .ctl_name = 0 },
  1633. };
  1634. static ctl_table fs_table[] = {
  1635. {
  1636. .ctl_name = FS_DQSTATS,
  1637. .procname = "quota",
  1638. .mode = 0555,
  1639. .child = fs_dqstats_table,
  1640. },
  1641. { .ctl_name = 0 },
  1642. };
  1643. static ctl_table sys_table[] = {
  1644. {
  1645. .ctl_name = CTL_FS,
  1646. .procname = "fs",
  1647. .mode = 0555,
  1648. .child = fs_table,
  1649. },
  1650. { .ctl_name = 0 },
  1651. };
  1652. static int __init dquot_init(void)
  1653. {
  1654. int i;
  1655. unsigned long nr_hash, order;
  1656. printk(KERN_NOTICE "VFS: Disk quotas %s\n", __DQUOT_VERSION__);
  1657. register_sysctl_table(sys_table, 0);
  1658. dquot_cachep = kmem_cache_create("dquot",
  1659. sizeof(struct dquot), sizeof(unsigned long) * 4,
  1660. (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
  1661. SLAB_MEM_SPREAD|SLAB_PANIC),
  1662. NULL, NULL);
  1663. order = 0;
  1664. dquot_hash = (struct hlist_head *)__get_free_pages(GFP_ATOMIC, order);
  1665. if (!dquot_hash)
  1666. panic("Cannot create dquot hash table");
  1667. /* Find power-of-two hlist_heads which can fit into allocation */
  1668. nr_hash = (1UL << order) * PAGE_SIZE / sizeof(struct hlist_head);
  1669. dq_hash_bits = 0;
  1670. do {
  1671. dq_hash_bits++;
  1672. } while (nr_hash >> dq_hash_bits);
  1673. dq_hash_bits--;
  1674. nr_hash = 1UL << dq_hash_bits;
  1675. dq_hash_mask = nr_hash - 1;
  1676. for (i = 0; i < nr_hash; i++)
  1677. INIT_HLIST_HEAD(dquot_hash + i);
  1678. printk("Dquot-cache hash table entries: %ld (order %ld, %ld bytes)\n",
  1679. nr_hash, order, (PAGE_SIZE << order));
  1680. set_shrinker(DEFAULT_SEEKS, shrink_dqcache_memory);
  1681. return 0;
  1682. }
  1683. module_init(dquot_init);
  1684. EXPORT_SYMBOL(register_quota_format);
  1685. EXPORT_SYMBOL(unregister_quota_format);
  1686. EXPORT_SYMBOL(dqstats);
  1687. EXPORT_SYMBOL(dq_data_lock);
  1688. EXPORT_SYMBOL(vfs_quota_on);
  1689. EXPORT_SYMBOL(vfs_quota_on_mount);
  1690. EXPORT_SYMBOL(vfs_quota_off);
  1691. EXPORT_SYMBOL(vfs_quota_sync);
  1692. EXPORT_SYMBOL(vfs_get_dqinfo);
  1693. EXPORT_SYMBOL(vfs_set_dqinfo);
  1694. EXPORT_SYMBOL(vfs_get_dqblk);
  1695. EXPORT_SYMBOL(vfs_set_dqblk);
  1696. EXPORT_SYMBOL(dquot_commit);
  1697. EXPORT_SYMBOL(dquot_commit_info);
  1698. EXPORT_SYMBOL(dquot_acquire);
  1699. EXPORT_SYMBOL(dquot_release);
  1700. EXPORT_SYMBOL(dquot_mark_dquot_dirty);
  1701. EXPORT_SYMBOL(dquot_initialize);
  1702. EXPORT_SYMBOL(dquot_drop);
  1703. EXPORT_SYMBOL(dquot_alloc_space);
  1704. EXPORT_SYMBOL(dquot_alloc_inode);
  1705. EXPORT_SYMBOL(dquot_free_space);
  1706. EXPORT_SYMBOL(dquot_free_inode);
  1707. EXPORT_SYMBOL(dquot_transfer);