dquot.c 58 KB

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