dquot.c 53 KB

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