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 1.1.1.1 2007/06/12 07:27:12 eyryu Exp $
  13. *
  14. * Author: Marco van Wieringen <mvw@planets.elm.net>
  15. *
  16. * Fixes: Dmitry Gorodchanin <pgmdsg@ibi.com>, 11 Feb 96
  17. *
  18. * Revised list management to avoid races
  19. * -- Bill Hawes, <whawes@star.net>, 9/98
  20. *
  21. * Fixed races in dquot_transfer(), dqget() and dquot_alloc_...().
  22. * As the consequence the locking was moved from dquot_decr_...(),
  23. * dquot_incr_...() to calling functions.
  24. * invalidate_dquots() now writes modified dquots.
  25. * Serialized quota_off() and quota_on() for mount point.
  26. * Fixed a few bugs in grow_dquots().
  27. * Fixed deadlock in write_dquot() - we no longer account quotas on
  28. * quota files
  29. * remove_dquot_ref() moved to inode.c - it now traverses through inodes
  30. * add_dquot_ref() restarts after blocking
  31. * Added check for bogus uid and fixed check for group in quotactl.
  32. * Jan Kara, <jack@suse.cz>, sponsored by SuSE CR, 10-11/99
  33. *
  34. * Used struct list_head instead of own list struct
  35. * Invalidation of referenced dquots is no longer possible
  36. * Improved free_dquots list management
  37. * Quota and i_blocks are now updated in one place to avoid races
  38. * Warnings are now delayed so we won't block in critical section
  39. * Write updated not to require dquot lock
  40. * Jan Kara, <jack@suse.cz>, 9/2000
  41. *
  42. * Added dynamic quota structure allocation
  43. * Jan Kara <jack@suse.cz> 12/2000
  44. *
  45. * Rewritten quota interface. Implemented new quota format and
  46. * formats registering.
  47. * Jan Kara, <jack@suse.cz>, 2001,2002
  48. *
  49. * New SMP locking.
  50. * Jan Kara, <jack@suse.cz>, 10/2002
  51. *
  52. * Added journalled quota support, fix lock inversion problems
  53. * Jan Kara, <jack@suse.cz>, 2003,2004
  54. *
  55. * (C) Copyright 1994 - 1997 Marco van Wieringen
  56. */
  57. #include <linux/errno.h>
  58. #include <linux/kernel.h>
  59. #include <linux/fs.h>
  60. #include <linux/mount.h>
  61. #include <linux/mm.h>
  62. #include <linux/time.h>
  63. #include <linux/types.h>
  64. #include <linux/string.h>
  65. #include <linux/fcntl.h>
  66. #include <linux/stat.h>
  67. #include <linux/tty.h>
  68. #include <linux/file.h>
  69. #include <linux/slab.h>
  70. #include <linux/sysctl.h>
  71. #include <linux/smp_lock.h>
  72. #include <linux/init.h>
  73. #include <linux/module.h>
  74. #include <linux/proc_fs.h>
  75. #include <linux/security.h>
  76. #include <linux/kmod.h>
  77. #include <linux/namei.h>
  78. #include <linux/buffer_head.h>
  79. #include <linux/capability.h>
  80. #include <linux/quotaops.h>
  81. #include <linux/writeback.h> /* for inode_lock, oddly enough.. */
  82. #include <asm/uaccess.h>
  83. #define __DQUOT_PARANOIA
  84. /*
  85. * There are two quota SMP locks. dq_list_lock protects all lists with quotas
  86. * and quota formats and also dqstats structure containing statistics about the
  87. * lists. dq_data_lock protects data from dq_dqb and also mem_dqinfo structures
  88. * and also guards consistency of dquot->dq_dqb with inode->i_blocks, i_bytes.
  89. * i_blocks and i_bytes updates itself are guarded by i_lock acquired directly
  90. * in inode_add_bytes() and inode_sub_bytes().
  91. *
  92. * The spinlock ordering is hence: dq_data_lock > dq_list_lock > i_lock
  93. *
  94. * Note that some things (eg. sb pointer, type, id) doesn't change during
  95. * the life of the dquot structure and so needn't to be protected by a lock
  96. *
  97. * Any operation working on dquots via inode pointers must hold dqptr_sem. If
  98. * operation is just reading pointers from inode (or not using them at all) the
  99. * read lock is enough. If pointers are altered function must hold write lock
  100. * (these locking rules also apply for S_NOQUOTA flag in the inode - note that
  101. * for altering the flag i_mutex is also needed). If operation is holding
  102. * reference to dquot in other way (e.g. quotactl ops) it must be guarded by
  103. * dqonoff_mutex.
  104. * This locking assures that:
  105. * a) update/access to dquot pointers in inode is serialized
  106. * b) everyone is guarded against invalidate_dquots()
  107. *
  108. * Each dquot has its dq_lock mutex. Locked dquots might not be referenced
  109. * from inodes (dquot_alloc_space() and such don't check the dq_lock).
  110. * Currently dquot is locked only when it is being read to memory (or space for
  111. * it is being allocated) on the first dqget() and when it is being released on
  112. * the last dqput(). The allocation and release oparations are serialized by
  113. * the dq_lock and by checking the use count in dquot_release(). Write
  114. * operations on dquots don't hold dq_lock as they copy data under dq_data_lock
  115. * spinlock to internal buffers before writing.
  116. *
  117. * Lock ordering (including related VFS locks) is the following:
  118. * i_mutex > dqonoff_sem > journal_lock > dqptr_sem > dquot->dq_lock >
  119. * dqio_mutex
  120. * i_mutex on quota files is special (it's below dqio_mutex)
  121. */
  122. static DEFINE_SPINLOCK(dq_list_lock);
  123. DEFINE_SPINLOCK(dq_data_lock);
  124. static char *quotatypes[] = INITQFNAMES;
  125. static struct quota_format_type *quota_formats; /* List of registered formats */
  126. static struct quota_module_name module_names[] = INIT_QUOTA_MODULE_NAMES;
  127. /* SLAB cache for dquot structures */
  128. static struct kmem_cache *dquot_cachep;
  129. int register_quota_format(struct quota_format_type *fmt)
  130. {
  131. spin_lock(&dq_list_lock);
  132. fmt->qf_next = quota_formats;
  133. quota_formats = fmt;
  134. spin_unlock(&dq_list_lock);
  135. return 0;
  136. }
  137. void unregister_quota_format(struct quota_format_type *fmt)
  138. {
  139. struct quota_format_type **actqf;
  140. spin_lock(&dq_list_lock);
  141. for (actqf = &quota_formats; *actqf && *actqf != fmt; actqf = &(*actqf)->qf_next);
  142. if (*actqf)
  143. *actqf = (*actqf)->qf_next;
  144. spin_unlock(&dq_list_lock);
  145. }
  146. static struct quota_format_type *find_quota_format(int id)
  147. {
  148. struct quota_format_type *actqf;
  149. spin_lock(&dq_list_lock);
  150. for (actqf = quota_formats; actqf && actqf->qf_fmt_id != id; actqf = actqf->qf_next);
  151. if (!actqf || !try_module_get(actqf->qf_owner)) {
  152. int qm;
  153. spin_unlock(&dq_list_lock);
  154. for (qm = 0; module_names[qm].qm_fmt_id && module_names[qm].qm_fmt_id != id; qm++);
  155. if (!module_names[qm].qm_fmt_id || request_module(module_names[qm].qm_mod_name))
  156. return NULL;
  157. spin_lock(&dq_list_lock);
  158. for (actqf = quota_formats; actqf && actqf->qf_fmt_id != id; actqf = actqf->qf_next);
  159. if (actqf && !try_module_get(actqf->qf_owner))
  160. actqf = NULL;
  161. }
  162. spin_unlock(&dq_list_lock);
  163. return actqf;
  164. }
  165. static void put_quota_format(struct quota_format_type *fmt)
  166. {
  167. module_put(fmt->qf_owner);
  168. }
  169. /*
  170. * Dquot List Management:
  171. * The quota code uses three lists for dquot management: the inuse_list,
  172. * free_dquots, and dquot_hash[] array. A single dquot structure may be
  173. * on all three lists, depending on its current state.
  174. *
  175. * All dquots are placed to the end of inuse_list when first created, and this
  176. * list is used for invalidate operation, which must look at every dquot.
  177. *
  178. * Unused dquots (dq_count == 0) are added to the free_dquots list when freed,
  179. * and this list is searched whenever we need an available dquot. Dquots are
  180. * removed from the list as soon as they are used again, and
  181. * dqstats.free_dquots gives the number of dquots on the list. When
  182. * dquot is invalidated it's completely released from memory.
  183. *
  184. * Dquots with a specific identity (device, type and id) are placed on
  185. * one of the dquot_hash[] hash chains. The provides an efficient search
  186. * mechanism to locate a specific dquot.
  187. */
  188. static LIST_HEAD(inuse_list);
  189. static LIST_HEAD(free_dquots);
  190. static unsigned int dq_hash_bits, dq_hash_mask;
  191. static struct hlist_head *dquot_hash;
  192. struct dqstats dqstats;
  193. static void dqput(struct dquot *dquot);
  194. static inline unsigned int
  195. hashfn(const struct super_block *sb, unsigned int id, int type)
  196. {
  197. unsigned long tmp;
  198. tmp = (((unsigned long)sb>>L1_CACHE_SHIFT) ^ id) * (MAXQUOTAS - type);
  199. return (tmp + (tmp >> dq_hash_bits)) & dq_hash_mask;
  200. }
  201. /*
  202. * Following list functions expect dq_list_lock to be held
  203. */
  204. static inline void insert_dquot_hash(struct dquot *dquot)
  205. {
  206. struct hlist_head *head = dquot_hash + hashfn(dquot->dq_sb, dquot->dq_id, dquot->dq_type);
  207. hlist_add_head(&dquot->dq_hash, head);
  208. }
  209. static inline void remove_dquot_hash(struct dquot *dquot)
  210. {
  211. hlist_del_init(&dquot->dq_hash);
  212. }
  213. static inline struct dquot *find_dquot(unsigned int hashent, struct super_block *sb, unsigned int id, int type)
  214. {
  215. struct hlist_node *node;
  216. struct dquot *dquot;
  217. hlist_for_each (node, dquot_hash+hashent) {
  218. dquot = hlist_entry(node, struct dquot, dq_hash);
  219. if (dquot->dq_sb == sb && dquot->dq_id == id && dquot->dq_type == type)
  220. return dquot;
  221. }
  222. return NODQUOT;
  223. }
  224. /* Add a dquot to the tail of the free list */
  225. static inline void put_dquot_last(struct dquot *dquot)
  226. {
  227. list_add_tail(&dquot->dq_free, &free_dquots);
  228. dqstats.free_dquots++;
  229. }
  230. static inline void remove_free_dquot(struct dquot *dquot)
  231. {
  232. if (list_empty(&dquot->dq_free))
  233. return;
  234. list_del_init(&dquot->dq_free);
  235. dqstats.free_dquots--;
  236. }
  237. static inline void put_inuse(struct dquot *dquot)
  238. {
  239. /* We add to the back of inuse list so we don't have to restart
  240. * when traversing this list and we block */
  241. list_add_tail(&dquot->dq_inuse, &inuse_list);
  242. dqstats.allocated_dquots++;
  243. }
  244. static inline void remove_inuse(struct dquot *dquot)
  245. {
  246. dqstats.allocated_dquots--;
  247. list_del(&dquot->dq_inuse);
  248. }
  249. /*
  250. * End of list functions needing dq_list_lock
  251. */
  252. static void wait_on_dquot(struct dquot *dquot)
  253. {
  254. mutex_lock(&dquot->dq_lock);
  255. mutex_unlock(&dquot->dq_lock);
  256. }
  257. #define mark_dquot_dirty(dquot) ((dquot)->dq_sb->dq_op->mark_dirty(dquot))
  258. int dquot_mark_dquot_dirty(struct dquot *dquot)
  259. {
  260. spin_lock(&dq_list_lock);
  261. if (!test_and_set_bit(DQ_MOD_B, &dquot->dq_flags))
  262. list_add(&dquot->dq_dirty, &sb_dqopt(dquot->dq_sb)->
  263. info[dquot->dq_type].dqi_dirty_list);
  264. spin_unlock(&dq_list_lock);
  265. return 0;
  266. }
  267. /* This function needs dq_list_lock */
  268. static inline int clear_dquot_dirty(struct dquot *dquot)
  269. {
  270. if (!test_and_clear_bit(DQ_MOD_B, &dquot->dq_flags))
  271. return 0;
  272. list_del_init(&dquot->dq_dirty);
  273. return 1;
  274. }
  275. void mark_info_dirty(struct super_block *sb, int type)
  276. {
  277. set_bit(DQF_INFO_DIRTY_B, &sb_dqopt(sb)->info[type].dqi_flags);
  278. }
  279. EXPORT_SYMBOL(mark_info_dirty);
  280. /*
  281. * Read dquot from disk and alloc space for it
  282. */
  283. int dquot_acquire(struct dquot *dquot)
  284. {
  285. int ret = 0, ret2 = 0;
  286. struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
  287. mutex_lock(&dquot->dq_lock);
  288. mutex_lock(&dqopt->dqio_mutex);
  289. if (!test_bit(DQ_READ_B, &dquot->dq_flags))
  290. ret = dqopt->ops[dquot->dq_type]->read_dqblk(dquot);
  291. if (ret < 0)
  292. goto out_iolock;
  293. set_bit(DQ_READ_B, &dquot->dq_flags);
  294. /* Instantiate dquot if needed */
  295. if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags) && !dquot->dq_off) {
  296. ret = dqopt->ops[dquot->dq_type]->commit_dqblk(dquot);
  297. /* Write the info if needed */
  298. if (info_dirty(&dqopt->info[dquot->dq_type]))
  299. ret2 = dqopt->ops[dquot->dq_type]->write_file_info(dquot->dq_sb, dquot->dq_type);
  300. if (ret < 0)
  301. goto out_iolock;
  302. if (ret2 < 0) {
  303. ret = ret2;
  304. goto out_iolock;
  305. }
  306. }
  307. set_bit(DQ_ACTIVE_B, &dquot->dq_flags);
  308. out_iolock:
  309. mutex_unlock(&dqopt->dqio_mutex);
  310. mutex_unlock(&dquot->dq_lock);
  311. return ret;
  312. }
  313. /*
  314. * Write dquot to disk
  315. */
  316. int dquot_commit(struct dquot *dquot)
  317. {
  318. int ret = 0, ret2 = 0;
  319. struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
  320. mutex_lock(&dqopt->dqio_mutex);
  321. spin_lock(&dq_list_lock);
  322. if (!clear_dquot_dirty(dquot)) {
  323. spin_unlock(&dq_list_lock);
  324. goto out_sem;
  325. }
  326. spin_unlock(&dq_list_lock);
  327. /* Inactive dquot can be only if there was error during read/init
  328. * => we have better not writing it */
  329. if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
  330. ret = dqopt->ops[dquot->dq_type]->commit_dqblk(dquot);
  331. if (info_dirty(&dqopt->info[dquot->dq_type]))
  332. ret2 = dqopt->ops[dquot->dq_type]->write_file_info(dquot->dq_sb, dquot->dq_type);
  333. if (ret >= 0)
  334. ret = ret2;
  335. }
  336. out_sem:
  337. mutex_unlock(&dqopt->dqio_mutex);
  338. return ret;
  339. }
  340. /*
  341. * Release dquot
  342. */
  343. int dquot_release(struct dquot *dquot)
  344. {
  345. int ret = 0, ret2 = 0;
  346. struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
  347. mutex_lock(&dquot->dq_lock);
  348. /* Check whether we are not racing with some other dqget() */
  349. if (atomic_read(&dquot->dq_count) > 1)
  350. goto out_dqlock;
  351. mutex_lock(&dqopt->dqio_mutex);
  352. if (dqopt->ops[dquot->dq_type]->release_dqblk) {
  353. ret = dqopt->ops[dquot->dq_type]->release_dqblk(dquot);
  354. /* Write the info */
  355. if (info_dirty(&dqopt->info[dquot->dq_type]))
  356. ret2 = dqopt->ops[dquot->dq_type]->write_file_info(dquot->dq_sb, dquot->dq_type);
  357. if (ret >= 0)
  358. ret = ret2;
  359. }
  360. clear_bit(DQ_ACTIVE_B, &dquot->dq_flags);
  361. mutex_unlock(&dqopt->dqio_mutex);
  362. out_dqlock:
  363. mutex_unlock(&dquot->dq_lock);
  364. return ret;
  365. }
  366. /* Invalidate all dquots on the list. Note that this function is called after
  367. * quota is disabled and pointers from inodes removed so there cannot be new
  368. * quota users. There can still be some users of quotas due to inodes being
  369. * just deleted or pruned by prune_icache() (those are not attached to any
  370. * list). We have to wait for such users.
  371. */
  372. static void invalidate_dquots(struct super_block *sb, int type)
  373. {
  374. struct dquot *dquot, *tmp;
  375. restart:
  376. spin_lock(&dq_list_lock);
  377. list_for_each_entry_safe(dquot, tmp, &inuse_list, dq_inuse) {
  378. if (dquot->dq_sb != sb)
  379. continue;
  380. if (dquot->dq_type != type)
  381. continue;
  382. /* Wait for dquot users */
  383. if (atomic_read(&dquot->dq_count)) {
  384. DEFINE_WAIT(wait);
  385. atomic_inc(&dquot->dq_count);
  386. prepare_to_wait(&dquot->dq_wait_unused, &wait,
  387. TASK_UNINTERRUPTIBLE);
  388. spin_unlock(&dq_list_lock);
  389. /* Once dqput() wakes us up, we know it's time to free
  390. * the dquot.
  391. * IMPORTANT: we rely on the fact that there is always
  392. * at most one process waiting for dquot to free.
  393. * Otherwise dq_count would be > 1 and we would never
  394. * wake up.
  395. */
  396. if (atomic_read(&dquot->dq_count) > 1)
  397. schedule();
  398. finish_wait(&dquot->dq_wait_unused, &wait);
  399. dqput(dquot);
  400. /* At this moment dquot() need not exist (it could be
  401. * reclaimed by prune_dqcache(). Hence we must
  402. * restart. */
  403. goto restart;
  404. }
  405. /*
  406. * Quota now has no users and it has been written on last
  407. * dqput()
  408. */
  409. remove_dquot_hash(dquot);
  410. remove_free_dquot(dquot);
  411. remove_inuse(dquot);
  412. kmem_cache_free(dquot_cachep, dquot);
  413. }
  414. spin_unlock(&dq_list_lock);
  415. }
  416. int vfs_quota_sync(struct super_block *sb, int type)
  417. {
  418. struct list_head *dirty;
  419. struct dquot *dquot;
  420. struct quota_info *dqopt = sb_dqopt(sb);
  421. int cnt;
  422. mutex_lock(&dqopt->dqonoff_mutex);
  423. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  424. if (type != -1 && cnt != type)
  425. continue;
  426. if (!sb_has_quota_enabled(sb, cnt))
  427. continue;
  428. spin_lock(&dq_list_lock);
  429. dirty = &dqopt->info[cnt].dqi_dirty_list;
  430. while (!list_empty(dirty)) {
  431. dquot = list_entry(dirty->next, struct dquot, dq_dirty);
  432. /* Dirty and inactive can be only bad dquot... */
  433. if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
  434. clear_dquot_dirty(dquot);
  435. continue;
  436. }
  437. /* Now we have active dquot from which someone is
  438. * holding reference so we can safely just increase
  439. * use count */
  440. atomic_inc(&dquot->dq_count);
  441. dqstats.lookups++;
  442. spin_unlock(&dq_list_lock);
  443. sb->dq_op->write_dquot(dquot);
  444. dqput(dquot);
  445. spin_lock(&dq_list_lock);
  446. }
  447. spin_unlock(&dq_list_lock);
  448. }
  449. for (cnt = 0; cnt < MAXQUOTAS; cnt++)
  450. if ((cnt == type || type == -1) && sb_has_quota_enabled(sb, cnt)
  451. && info_dirty(&dqopt->info[cnt]))
  452. sb->dq_op->write_info(sb, cnt);
  453. spin_lock(&dq_list_lock);
  454. dqstats.syncs++;
  455. spin_unlock(&dq_list_lock);
  456. mutex_unlock(&dqopt->dqonoff_mutex);
  457. return 0;
  458. }
  459. /* Free unused dquots from cache */
  460. static void prune_dqcache(int count)
  461. {
  462. struct list_head *head;
  463. struct dquot *dquot;
  464. head = free_dquots.prev;
  465. while (head != &free_dquots && count) {
  466. dquot = list_entry(head, struct dquot, dq_free);
  467. remove_dquot_hash(dquot);
  468. remove_free_dquot(dquot);
  469. remove_inuse(dquot);
  470. kmem_cache_free(dquot_cachep, dquot);
  471. count--;
  472. head = free_dquots.prev;
  473. }
  474. }
  475. /*
  476. * This is called from kswapd when we think we need some
  477. * more memory
  478. */
  479. static int shrink_dqcache_memory(int nr, gfp_t gfp_mask)
  480. {
  481. if (nr) {
  482. spin_lock(&dq_list_lock);
  483. prune_dqcache(nr);
  484. spin_unlock(&dq_list_lock);
  485. }
  486. return (dqstats.free_dquots / 100) * sysctl_vfs_cache_pressure;
  487. }
  488. /*
  489. * Put reference to dquot
  490. * NOTE: If you change this function please check whether dqput_blocks() works right...
  491. * MUST be called with either dqptr_sem or dqonoff_mutex held
  492. */
  493. static void dqput(struct dquot *dquot)
  494. {
  495. if (!dquot)
  496. return;
  497. #ifdef __DQUOT_PARANOIA
  498. if (!atomic_read(&dquot->dq_count)) {
  499. printk("VFS: dqput: trying to free free dquot\n");
  500. printk("VFS: device %s, dquot of %s %d\n",
  501. dquot->dq_sb->s_id,
  502. quotatypes[dquot->dq_type],
  503. dquot->dq_id);
  504. BUG();
  505. }
  506. #endif
  507. spin_lock(&dq_list_lock);
  508. dqstats.drops++;
  509. spin_unlock(&dq_list_lock);
  510. we_slept:
  511. spin_lock(&dq_list_lock);
  512. if (atomic_read(&dquot->dq_count) > 1) {
  513. /* We have more than one user... nothing to do */
  514. atomic_dec(&dquot->dq_count);
  515. /* Releasing dquot during quotaoff phase? */
  516. if (!sb_has_quota_enabled(dquot->dq_sb, dquot->dq_type) &&
  517. atomic_read(&dquot->dq_count) == 1)
  518. wake_up(&dquot->dq_wait_unused);
  519. spin_unlock(&dq_list_lock);
  520. return;
  521. }
  522. /* Need to release dquot? */
  523. if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags) && dquot_dirty(dquot)) {
  524. spin_unlock(&dq_list_lock);
  525. /* Commit dquot before releasing */
  526. dquot->dq_sb->dq_op->write_dquot(dquot);
  527. goto we_slept;
  528. }
  529. /* Clear flag in case dquot was inactive (something bad happened) */
  530. clear_dquot_dirty(dquot);
  531. if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
  532. spin_unlock(&dq_list_lock);
  533. dquot->dq_sb->dq_op->release_dquot(dquot);
  534. goto we_slept;
  535. }
  536. atomic_dec(&dquot->dq_count);
  537. #ifdef __DQUOT_PARANOIA
  538. /* sanity check */
  539. BUG_ON(!list_empty(&dquot->dq_free));
  540. #endif
  541. put_dquot_last(dquot);
  542. spin_unlock(&dq_list_lock);
  543. }
  544. static struct dquot *get_empty_dquot(struct super_block *sb, int type)
  545. {
  546. struct dquot *dquot;
  547. dquot = kmem_cache_zalloc(dquot_cachep, GFP_NOFS);
  548. if(!dquot)
  549. return NODQUOT;
  550. mutex_init(&dquot->dq_lock);
  551. INIT_LIST_HEAD(&dquot->dq_free);
  552. INIT_LIST_HEAD(&dquot->dq_inuse);
  553. INIT_HLIST_NODE(&dquot->dq_hash);
  554. INIT_LIST_HEAD(&dquot->dq_dirty);
  555. init_waitqueue_head(&dquot->dq_wait_unused);
  556. dquot->dq_sb = sb;
  557. dquot->dq_type = type;
  558. atomic_set(&dquot->dq_count, 1);
  559. return dquot;
  560. }
  561. /*
  562. * Get reference to dquot
  563. * MUST be called with either dqptr_sem or dqonoff_mutex held
  564. */
  565. static struct dquot *dqget(struct super_block *sb, unsigned int id, int type)
  566. {
  567. unsigned int hashent = hashfn(sb, id, type);
  568. struct dquot *dquot, *empty = NODQUOT;
  569. if (!sb_has_quota_enabled(sb, type))
  570. return NODQUOT;
  571. we_slept:
  572. spin_lock(&dq_list_lock);
  573. if ((dquot = find_dquot(hashent, sb, id, type)) == NODQUOT) {
  574. if (empty == NODQUOT) {
  575. spin_unlock(&dq_list_lock);
  576. if ((empty = get_empty_dquot(sb, type)) == NODQUOT)
  577. schedule(); /* Try to wait for a moment... */
  578. goto we_slept;
  579. }
  580. dquot = empty;
  581. dquot->dq_id = id;
  582. /* all dquots go on the inuse_list */
  583. put_inuse(dquot);
  584. /* hash it first so it can be found */
  585. insert_dquot_hash(dquot);
  586. dqstats.lookups++;
  587. spin_unlock(&dq_list_lock);
  588. } else {
  589. if (!atomic_read(&dquot->dq_count))
  590. remove_free_dquot(dquot);
  591. atomic_inc(&dquot->dq_count);
  592. dqstats.cache_hits++;
  593. dqstats.lookups++;
  594. spin_unlock(&dq_list_lock);
  595. if (empty)
  596. kmem_cache_free(dquot_cachep, empty);
  597. }
  598. /* Wait for dq_lock - after this we know that either dquot_release() is already
  599. * finished or it will be canceled due to dq_count > 1 test */
  600. wait_on_dquot(dquot);
  601. /* Read the dquot and instantiate it (everything done only if needed) */
  602. if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags) && sb->dq_op->acquire_dquot(dquot) < 0) {
  603. dqput(dquot);
  604. return NODQUOT;
  605. }
  606. #ifdef __DQUOT_PARANOIA
  607. BUG_ON(!dquot->dq_sb); /* Has somebody invalidated entry under us? */
  608. #endif
  609. return dquot;
  610. }
  611. static int dqinit_needed(struct inode *inode, int type)
  612. {
  613. int cnt;
  614. if (IS_NOQUOTA(inode))
  615. return 0;
  616. if (type != -1)
  617. return inode->i_dquot[type] == NODQUOT;
  618. for (cnt = 0; cnt < MAXQUOTAS; cnt++)
  619. if (inode->i_dquot[cnt] == NODQUOT)
  620. return 1;
  621. return 0;
  622. }
  623. /* This routine is guarded by dqonoff_mutex mutex */
  624. static void add_dquot_ref(struct super_block *sb, int type)
  625. {
  626. struct inode *inode;
  627. restart:
  628. spin_lock(&inode_lock);
  629. list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
  630. if (!atomic_read(&inode->i_writecount))
  631. continue;
  632. if (!dqinit_needed(inode, type))
  633. continue;
  634. if (inode->i_state & (I_FREEING|I_WILL_FREE))
  635. continue;
  636. __iget(inode);
  637. spin_unlock(&inode_lock);
  638. sb->dq_op->initialize(inode, type);
  639. iput(inode);
  640. /* As we may have blocked we had better restart... */
  641. goto restart;
  642. }
  643. spin_unlock(&inode_lock);
  644. }
  645. /* Return 0 if dqput() won't block (note that 1 doesn't necessarily mean blocking) */
  646. static inline int dqput_blocks(struct dquot *dquot)
  647. {
  648. if (atomic_read(&dquot->dq_count) <= 1)
  649. return 1;
  650. return 0;
  651. }
  652. /* Remove references to dquots from inode - add dquot to list for freeing if needed */
  653. /* We can't race with anybody because we hold dqptr_sem for writing... */
  654. int remove_inode_dquot_ref(struct inode *inode, int type, struct list_head *tofree_head)
  655. {
  656. struct dquot *dquot = inode->i_dquot[type];
  657. inode->i_dquot[type] = NODQUOT;
  658. if (dquot != NODQUOT) {
  659. if (dqput_blocks(dquot)) {
  660. #ifdef __DQUOT_PARANOIA
  661. if (atomic_read(&dquot->dq_count) != 1)
  662. printk(KERN_WARNING "VFS: Adding dquot with dq_count %d to dispose list.\n", atomic_read(&dquot->dq_count));
  663. #endif
  664. spin_lock(&dq_list_lock);
  665. list_add(&dquot->dq_free, tofree_head); /* As dquot must have currently users it can't be on the free list... */
  666. spin_unlock(&dq_list_lock);
  667. return 1;
  668. }
  669. else
  670. dqput(dquot); /* We have guaranteed we won't block */
  671. }
  672. return 0;
  673. }
  674. /* Free list of dquots - called from inode.c */
  675. /* dquots are removed from inodes, no new references can be got so we are the only ones holding reference */
  676. static void put_dquot_list(struct list_head *tofree_head)
  677. {
  678. struct list_head *act_head;
  679. struct dquot *dquot;
  680. act_head = tofree_head->next;
  681. /* So now we have dquots on the list... Just free them */
  682. while (act_head != tofree_head) {
  683. dquot = list_entry(act_head, struct dquot, dq_free);
  684. act_head = act_head->next;
  685. list_del_init(&dquot->dq_free); /* Remove dquot from the list so we won't have problems... */
  686. dqput(dquot);
  687. }
  688. }
  689. 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, 0);
  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, 0);
  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);