dquot.c 52 KB

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