dquot.c 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004
  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. #define mark_dquot_dirty(dquot) ((dquot)->dq_sb->dq_op->mark_dirty(dquot))
  261. int dquot_mark_dquot_dirty(struct dquot *dquot)
  262. {
  263. spin_lock(&dq_list_lock);
  264. if (!test_and_set_bit(DQ_MOD_B, &dquot->dq_flags))
  265. list_add(&dquot->dq_dirty, &sb_dqopt(dquot->dq_sb)->
  266. info[dquot->dq_type].dqi_dirty_list);
  267. spin_unlock(&dq_list_lock);
  268. return 0;
  269. }
  270. /* This function needs dq_list_lock */
  271. static inline int clear_dquot_dirty(struct dquot *dquot)
  272. {
  273. if (!test_and_clear_bit(DQ_MOD_B, &dquot->dq_flags))
  274. return 0;
  275. list_del_init(&dquot->dq_dirty);
  276. return 1;
  277. }
  278. void mark_info_dirty(struct super_block *sb, int type)
  279. {
  280. set_bit(DQF_INFO_DIRTY_B, &sb_dqopt(sb)->info[type].dqi_flags);
  281. }
  282. EXPORT_SYMBOL(mark_info_dirty);
  283. /*
  284. * Read dquot from disk and alloc space for it
  285. */
  286. int dquot_acquire(struct dquot *dquot)
  287. {
  288. int ret = 0, ret2 = 0;
  289. struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
  290. mutex_lock(&dquot->dq_lock);
  291. mutex_lock(&dqopt->dqio_mutex);
  292. if (!test_bit(DQ_READ_B, &dquot->dq_flags))
  293. ret = dqopt->ops[dquot->dq_type]->read_dqblk(dquot);
  294. if (ret < 0)
  295. goto out_iolock;
  296. set_bit(DQ_READ_B, &dquot->dq_flags);
  297. /* Instantiate dquot if needed */
  298. if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags) && !dquot->dq_off) {
  299. ret = dqopt->ops[dquot->dq_type]->commit_dqblk(dquot);
  300. /* Write the info if needed */
  301. if (info_dirty(&dqopt->info[dquot->dq_type]))
  302. ret2 = dqopt->ops[dquot->dq_type]->write_file_info(dquot->dq_sb, dquot->dq_type);
  303. if (ret < 0)
  304. goto out_iolock;
  305. if (ret2 < 0) {
  306. ret = ret2;
  307. goto out_iolock;
  308. }
  309. }
  310. set_bit(DQ_ACTIVE_B, &dquot->dq_flags);
  311. out_iolock:
  312. mutex_unlock(&dqopt->dqio_mutex);
  313. mutex_unlock(&dquot->dq_lock);
  314. return ret;
  315. }
  316. /*
  317. * Write dquot to disk
  318. */
  319. int dquot_commit(struct dquot *dquot)
  320. {
  321. int ret = 0, ret2 = 0;
  322. struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
  323. mutex_lock(&dqopt->dqio_mutex);
  324. spin_lock(&dq_list_lock);
  325. if (!clear_dquot_dirty(dquot)) {
  326. spin_unlock(&dq_list_lock);
  327. goto out_sem;
  328. }
  329. spin_unlock(&dq_list_lock);
  330. /* Inactive dquot can be only if there was error during read/init
  331. * => we have better not writing it */
  332. if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
  333. ret = dqopt->ops[dquot->dq_type]->commit_dqblk(dquot);
  334. if (info_dirty(&dqopt->info[dquot->dq_type]))
  335. ret2 = dqopt->ops[dquot->dq_type]->write_file_info(dquot->dq_sb, dquot->dq_type);
  336. if (ret >= 0)
  337. ret = ret2;
  338. }
  339. out_sem:
  340. mutex_unlock(&dqopt->dqio_mutex);
  341. return ret;
  342. }
  343. /*
  344. * Release dquot
  345. */
  346. int dquot_release(struct dquot *dquot)
  347. {
  348. int ret = 0, ret2 = 0;
  349. struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
  350. mutex_lock(&dquot->dq_lock);
  351. /* Check whether we are not racing with some other dqget() */
  352. if (atomic_read(&dquot->dq_count) > 1)
  353. goto out_dqlock;
  354. mutex_lock(&dqopt->dqio_mutex);
  355. if (dqopt->ops[dquot->dq_type]->release_dqblk) {
  356. ret = dqopt->ops[dquot->dq_type]->release_dqblk(dquot);
  357. /* Write the info */
  358. if (info_dirty(&dqopt->info[dquot->dq_type]))
  359. ret2 = dqopt->ops[dquot->dq_type]->write_file_info(dquot->dq_sb, dquot->dq_type);
  360. if (ret >= 0)
  361. ret = ret2;
  362. }
  363. clear_bit(DQ_ACTIVE_B, &dquot->dq_flags);
  364. mutex_unlock(&dqopt->dqio_mutex);
  365. out_dqlock:
  366. mutex_unlock(&dquot->dq_lock);
  367. return ret;
  368. }
  369. /* Invalidate all dquots on the list. Note that this function is called after
  370. * quota is disabled and pointers from inodes removed so there cannot be new
  371. * quota users. There can still be some users of quotas due to inodes being
  372. * just deleted or pruned by prune_icache() (those are not attached to any
  373. * list). We have to wait for such users.
  374. */
  375. static void invalidate_dquots(struct super_block *sb, int type)
  376. {
  377. struct dquot *dquot, *tmp;
  378. restart:
  379. spin_lock(&dq_list_lock);
  380. list_for_each_entry_safe(dquot, tmp, &inuse_list, dq_inuse) {
  381. if (dquot->dq_sb != sb)
  382. continue;
  383. if (dquot->dq_type != type)
  384. continue;
  385. /* Wait for dquot users */
  386. if (atomic_read(&dquot->dq_count)) {
  387. DEFINE_WAIT(wait);
  388. atomic_inc(&dquot->dq_count);
  389. prepare_to_wait(&dquot->dq_wait_unused, &wait,
  390. TASK_UNINTERRUPTIBLE);
  391. spin_unlock(&dq_list_lock);
  392. /* Once dqput() wakes us up, we know it's time to free
  393. * the dquot.
  394. * IMPORTANT: we rely on the fact that there is always
  395. * at most one process waiting for dquot to free.
  396. * Otherwise dq_count would be > 1 and we would never
  397. * wake up.
  398. */
  399. if (atomic_read(&dquot->dq_count) > 1)
  400. schedule();
  401. finish_wait(&dquot->dq_wait_unused, &wait);
  402. dqput(dquot);
  403. /* At this moment dquot() need not exist (it could be
  404. * reclaimed by prune_dqcache(). Hence we must
  405. * restart. */
  406. goto restart;
  407. }
  408. /*
  409. * Quota now has no users and it has been written on last
  410. * dqput()
  411. */
  412. remove_dquot_hash(dquot);
  413. remove_free_dquot(dquot);
  414. remove_inuse(dquot);
  415. kmem_cache_free(dquot_cachep, dquot);
  416. }
  417. spin_unlock(&dq_list_lock);
  418. }
  419. int vfs_quota_sync(struct super_block *sb, int type)
  420. {
  421. struct list_head *dirty;
  422. struct dquot *dquot;
  423. struct quota_info *dqopt = sb_dqopt(sb);
  424. int cnt;
  425. mutex_lock(&dqopt->dqonoff_mutex);
  426. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  427. if (type != -1 && cnt != type)
  428. continue;
  429. if (!sb_has_quota_enabled(sb, cnt))
  430. continue;
  431. spin_lock(&dq_list_lock);
  432. dirty = &dqopt->info[cnt].dqi_dirty_list;
  433. while (!list_empty(dirty)) {
  434. dquot = list_first_entry(dirty, struct dquot, dq_dirty);
  435. /* Dirty and inactive can be only bad dquot... */
  436. if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
  437. clear_dquot_dirty(dquot);
  438. continue;
  439. }
  440. /* Now we have active dquot from which someone is
  441. * holding reference so we can safely just increase
  442. * use count */
  443. atomic_inc(&dquot->dq_count);
  444. dqstats.lookups++;
  445. spin_unlock(&dq_list_lock);
  446. sb->dq_op->write_dquot(dquot);
  447. dqput(dquot);
  448. spin_lock(&dq_list_lock);
  449. }
  450. spin_unlock(&dq_list_lock);
  451. }
  452. for (cnt = 0; cnt < MAXQUOTAS; cnt++)
  453. if ((cnt == type || type == -1) && sb_has_quota_enabled(sb, cnt)
  454. && info_dirty(&dqopt->info[cnt]))
  455. sb->dq_op->write_info(sb, cnt);
  456. spin_lock(&dq_list_lock);
  457. dqstats.syncs++;
  458. spin_unlock(&dq_list_lock);
  459. mutex_unlock(&dqopt->dqonoff_mutex);
  460. return 0;
  461. }
  462. /* Free unused dquots from cache */
  463. static void prune_dqcache(int count)
  464. {
  465. struct list_head *head;
  466. struct dquot *dquot;
  467. head = free_dquots.prev;
  468. while (head != &free_dquots && count) {
  469. dquot = list_entry(head, struct dquot, dq_free);
  470. remove_dquot_hash(dquot);
  471. remove_free_dquot(dquot);
  472. remove_inuse(dquot);
  473. kmem_cache_free(dquot_cachep, dquot);
  474. count--;
  475. head = free_dquots.prev;
  476. }
  477. }
  478. /*
  479. * This is called from kswapd when we think we need some
  480. * more memory
  481. */
  482. static int shrink_dqcache_memory(int nr, gfp_t gfp_mask)
  483. {
  484. if (nr) {
  485. spin_lock(&dq_list_lock);
  486. prune_dqcache(nr);
  487. spin_unlock(&dq_list_lock);
  488. }
  489. return (dqstats.free_dquots / 100) * sysctl_vfs_cache_pressure;
  490. }
  491. static struct shrinker dqcache_shrinker = {
  492. .shrink = shrink_dqcache_memory,
  493. .seeks = DEFAULT_SEEKS,
  494. };
  495. /*
  496. * Put reference to dquot
  497. * NOTE: If you change this function please check whether dqput_blocks() works right...
  498. * MUST be called with either dqptr_sem or dqonoff_mutex held
  499. */
  500. static void dqput(struct dquot *dquot)
  501. {
  502. if (!dquot)
  503. return;
  504. #ifdef __DQUOT_PARANOIA
  505. if (!atomic_read(&dquot->dq_count)) {
  506. printk("VFS: dqput: trying to free free dquot\n");
  507. printk("VFS: device %s, dquot of %s %d\n",
  508. dquot->dq_sb->s_id,
  509. quotatypes[dquot->dq_type],
  510. dquot->dq_id);
  511. BUG();
  512. }
  513. #endif
  514. spin_lock(&dq_list_lock);
  515. dqstats.drops++;
  516. spin_unlock(&dq_list_lock);
  517. we_slept:
  518. spin_lock(&dq_list_lock);
  519. if (atomic_read(&dquot->dq_count) > 1) {
  520. /* We have more than one user... nothing to do */
  521. atomic_dec(&dquot->dq_count);
  522. /* Releasing dquot during quotaoff phase? */
  523. if (!sb_has_quota_enabled(dquot->dq_sb, dquot->dq_type) &&
  524. atomic_read(&dquot->dq_count) == 1)
  525. wake_up(&dquot->dq_wait_unused);
  526. spin_unlock(&dq_list_lock);
  527. return;
  528. }
  529. /* Need to release dquot? */
  530. if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags) && dquot_dirty(dquot)) {
  531. spin_unlock(&dq_list_lock);
  532. /* Commit dquot before releasing */
  533. dquot->dq_sb->dq_op->write_dquot(dquot);
  534. goto we_slept;
  535. }
  536. /* Clear flag in case dquot was inactive (something bad happened) */
  537. clear_dquot_dirty(dquot);
  538. if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
  539. spin_unlock(&dq_list_lock);
  540. dquot->dq_sb->dq_op->release_dquot(dquot);
  541. goto we_slept;
  542. }
  543. atomic_dec(&dquot->dq_count);
  544. #ifdef __DQUOT_PARANOIA
  545. /* sanity check */
  546. BUG_ON(!list_empty(&dquot->dq_free));
  547. #endif
  548. put_dquot_last(dquot);
  549. spin_unlock(&dq_list_lock);
  550. }
  551. static struct dquot *get_empty_dquot(struct super_block *sb, int type)
  552. {
  553. struct dquot *dquot;
  554. dquot = kmem_cache_zalloc(dquot_cachep, GFP_NOFS);
  555. if(!dquot)
  556. return NODQUOT;
  557. mutex_init(&dquot->dq_lock);
  558. INIT_LIST_HEAD(&dquot->dq_free);
  559. INIT_LIST_HEAD(&dquot->dq_inuse);
  560. INIT_HLIST_NODE(&dquot->dq_hash);
  561. INIT_LIST_HEAD(&dquot->dq_dirty);
  562. init_waitqueue_head(&dquot->dq_wait_unused);
  563. dquot->dq_sb = sb;
  564. dquot->dq_type = type;
  565. atomic_set(&dquot->dq_count, 1);
  566. return dquot;
  567. }
  568. /*
  569. * Get reference to dquot
  570. * MUST be called with either dqptr_sem or dqonoff_mutex held
  571. */
  572. static struct dquot *dqget(struct super_block *sb, unsigned int id, int type)
  573. {
  574. unsigned int hashent = hashfn(sb, id, type);
  575. struct dquot *dquot, *empty = NODQUOT;
  576. if (!sb_has_quota_enabled(sb, type))
  577. return NODQUOT;
  578. we_slept:
  579. spin_lock(&dq_list_lock);
  580. if ((dquot = find_dquot(hashent, sb, id, type)) == NODQUOT) {
  581. if (empty == NODQUOT) {
  582. spin_unlock(&dq_list_lock);
  583. if ((empty = get_empty_dquot(sb, type)) == NODQUOT)
  584. schedule(); /* Try to wait for a moment... */
  585. goto we_slept;
  586. }
  587. dquot = empty;
  588. dquot->dq_id = id;
  589. /* all dquots go on the inuse_list */
  590. put_inuse(dquot);
  591. /* hash it first so it can be found */
  592. insert_dquot_hash(dquot);
  593. dqstats.lookups++;
  594. spin_unlock(&dq_list_lock);
  595. } else {
  596. if (!atomic_read(&dquot->dq_count))
  597. remove_free_dquot(dquot);
  598. atomic_inc(&dquot->dq_count);
  599. dqstats.cache_hits++;
  600. dqstats.lookups++;
  601. spin_unlock(&dq_list_lock);
  602. if (empty)
  603. kmem_cache_free(dquot_cachep, empty);
  604. }
  605. /* Wait for dq_lock - after this we know that either dquot_release() is already
  606. * finished or it will be canceled due to dq_count > 1 test */
  607. wait_on_dquot(dquot);
  608. /* Read the dquot and instantiate it (everything done only if needed) */
  609. if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags) && sb->dq_op->acquire_dquot(dquot) < 0) {
  610. dqput(dquot);
  611. return NODQUOT;
  612. }
  613. #ifdef __DQUOT_PARANOIA
  614. BUG_ON(!dquot->dq_sb); /* Has somebody invalidated entry under us? */
  615. #endif
  616. return dquot;
  617. }
  618. static int dqinit_needed(struct inode *inode, int type)
  619. {
  620. int cnt;
  621. if (IS_NOQUOTA(inode))
  622. return 0;
  623. if (type != -1)
  624. return inode->i_dquot[type] == NODQUOT;
  625. for (cnt = 0; cnt < MAXQUOTAS; cnt++)
  626. if (inode->i_dquot[cnt] == NODQUOT)
  627. return 1;
  628. return 0;
  629. }
  630. /* This routine is guarded by dqonoff_mutex mutex */
  631. static void add_dquot_ref(struct super_block *sb, int type)
  632. {
  633. struct inode *inode, *old_inode = NULL;
  634. spin_lock(&inode_lock);
  635. list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
  636. if (!atomic_read(&inode->i_writecount))
  637. continue;
  638. if (!dqinit_needed(inode, type))
  639. continue;
  640. if (inode->i_state & (I_FREEING|I_WILL_FREE))
  641. continue;
  642. __iget(inode);
  643. spin_unlock(&inode_lock);
  644. iput(old_inode);
  645. sb->dq_op->initialize(inode, type);
  646. /* We hold a reference to 'inode' so it couldn't have been
  647. * removed from s_inodes list while we dropped the inode_lock.
  648. * We cannot iput the inode now as we can be holding the last
  649. * reference and we cannot iput it under inode_lock. So we
  650. * keep the reference and iput it later. */
  651. old_inode = inode;
  652. spin_lock(&inode_lock);
  653. }
  654. spin_unlock(&inode_lock);
  655. iput(old_inode);
  656. }
  657. /* Return 0 if dqput() won't block (note that 1 doesn't necessarily mean blocking) */
  658. static inline int dqput_blocks(struct dquot *dquot)
  659. {
  660. if (atomic_read(&dquot->dq_count) <= 1)
  661. return 1;
  662. return 0;
  663. }
  664. /* Remove references to dquots from inode - add dquot to list for freeing if needed */
  665. /* We can't race with anybody because we hold dqptr_sem for writing... */
  666. static int remove_inode_dquot_ref(struct inode *inode, int type,
  667. struct list_head *tofree_head)
  668. {
  669. struct dquot *dquot = inode->i_dquot[type];
  670. inode->i_dquot[type] = NODQUOT;
  671. if (dquot != NODQUOT) {
  672. if (dqput_blocks(dquot)) {
  673. #ifdef __DQUOT_PARANOIA
  674. if (atomic_read(&dquot->dq_count) != 1)
  675. printk(KERN_WARNING "VFS: Adding dquot with dq_count %d to dispose list.\n", atomic_read(&dquot->dq_count));
  676. #endif
  677. spin_lock(&dq_list_lock);
  678. list_add(&dquot->dq_free, tofree_head); /* As dquot must have currently users it can't be on the free list... */
  679. spin_unlock(&dq_list_lock);
  680. return 1;
  681. }
  682. else
  683. dqput(dquot); /* We have guaranteed we won't block */
  684. }
  685. return 0;
  686. }
  687. /* Free list of dquots - called from inode.c */
  688. /* dquots are removed from inodes, no new references can be got so we are the only ones holding reference */
  689. static void put_dquot_list(struct list_head *tofree_head)
  690. {
  691. struct list_head *act_head;
  692. struct dquot *dquot;
  693. act_head = tofree_head->next;
  694. /* So now we have dquots on the list... Just free them */
  695. while (act_head != tofree_head) {
  696. dquot = list_entry(act_head, struct dquot, dq_free);
  697. act_head = act_head->next;
  698. list_del_init(&dquot->dq_free); /* Remove dquot from the list so we won't have problems... */
  699. dqput(dquot);
  700. }
  701. }
  702. static void remove_dquot_ref(struct super_block *sb, int type,
  703. struct list_head *tofree_head)
  704. {
  705. struct inode *inode;
  706. spin_lock(&inode_lock);
  707. list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
  708. if (!IS_NOQUOTA(inode))
  709. remove_inode_dquot_ref(inode, type, tofree_head);
  710. }
  711. spin_unlock(&inode_lock);
  712. }
  713. /* Gather all references from inodes and drop them */
  714. static void drop_dquot_ref(struct super_block *sb, int type)
  715. {
  716. LIST_HEAD(tofree_head);
  717. if (sb->dq_op) {
  718. down_write(&sb_dqopt(sb)->dqptr_sem);
  719. remove_dquot_ref(sb, type, &tofree_head);
  720. up_write(&sb_dqopt(sb)->dqptr_sem);
  721. put_dquot_list(&tofree_head);
  722. }
  723. }
  724. static inline void dquot_incr_inodes(struct dquot *dquot, unsigned long number)
  725. {
  726. dquot->dq_dqb.dqb_curinodes += number;
  727. }
  728. static inline void dquot_incr_space(struct dquot *dquot, qsize_t number)
  729. {
  730. dquot->dq_dqb.dqb_curspace += number;
  731. }
  732. static inline void dquot_decr_inodes(struct dquot *dquot, unsigned long number)
  733. {
  734. if (dquot->dq_dqb.dqb_curinodes > number)
  735. dquot->dq_dqb.dqb_curinodes -= number;
  736. else
  737. dquot->dq_dqb.dqb_curinodes = 0;
  738. if (dquot->dq_dqb.dqb_curinodes <= dquot->dq_dqb.dqb_isoftlimit)
  739. dquot->dq_dqb.dqb_itime = (time_t) 0;
  740. clear_bit(DQ_INODES_B, &dquot->dq_flags);
  741. }
  742. static inline void dquot_decr_space(struct dquot *dquot, qsize_t number)
  743. {
  744. if (dquot->dq_dqb.dqb_curspace > number)
  745. dquot->dq_dqb.dqb_curspace -= number;
  746. else
  747. dquot->dq_dqb.dqb_curspace = 0;
  748. if (toqb(dquot->dq_dqb.dqb_curspace) <= dquot->dq_dqb.dqb_bsoftlimit)
  749. dquot->dq_dqb.dqb_btime = (time_t) 0;
  750. clear_bit(DQ_BLKS_B, &dquot->dq_flags);
  751. }
  752. static int warning_issued(struct dquot *dquot, const int warntype)
  753. {
  754. int flag = (warntype == QUOTA_NL_BHARDWARN ||
  755. warntype == QUOTA_NL_BSOFTLONGWARN) ? DQ_BLKS_B :
  756. ((warntype == QUOTA_NL_IHARDWARN ||
  757. warntype == QUOTA_NL_ISOFTLONGWARN) ? DQ_INODES_B : 0);
  758. if (!flag)
  759. return 0;
  760. return test_and_set_bit(flag, &dquot->dq_flags);
  761. }
  762. #ifdef CONFIG_PRINT_QUOTA_WARNING
  763. static int flag_print_warnings = 1;
  764. static inline int need_print_warning(struct dquot *dquot)
  765. {
  766. if (!flag_print_warnings)
  767. return 0;
  768. switch (dquot->dq_type) {
  769. case USRQUOTA:
  770. return current->fsuid == dquot->dq_id;
  771. case GRPQUOTA:
  772. return in_group_p(dquot->dq_id);
  773. }
  774. return 0;
  775. }
  776. /* Print warning to user which exceeded quota */
  777. static void print_warning(struct dquot *dquot, const int warntype)
  778. {
  779. char *msg = NULL;
  780. struct tty_struct *tty;
  781. if (!need_print_warning(dquot))
  782. return;
  783. mutex_lock(&tty_mutex);
  784. tty = get_current_tty();
  785. if (!tty)
  786. goto out_lock;
  787. tty_write_message(tty, dquot->dq_sb->s_id);
  788. if (warntype == QUOTA_NL_ISOFTWARN || warntype == QUOTA_NL_BSOFTWARN)
  789. tty_write_message(tty, ": warning, ");
  790. else
  791. tty_write_message(tty, ": write failed, ");
  792. tty_write_message(tty, quotatypes[dquot->dq_type]);
  793. switch (warntype) {
  794. case QUOTA_NL_IHARDWARN:
  795. msg = " file limit reached.\r\n";
  796. break;
  797. case QUOTA_NL_ISOFTLONGWARN:
  798. msg = " file quota exceeded too long.\r\n";
  799. break;
  800. case QUOTA_NL_ISOFTWARN:
  801. msg = " file quota exceeded.\r\n";
  802. break;
  803. case QUOTA_NL_BHARDWARN:
  804. msg = " block limit reached.\r\n";
  805. break;
  806. case QUOTA_NL_BSOFTLONGWARN:
  807. msg = " block quota exceeded too long.\r\n";
  808. break;
  809. case QUOTA_NL_BSOFTWARN:
  810. msg = " block quota exceeded.\r\n";
  811. break;
  812. }
  813. tty_write_message(tty, msg);
  814. out_lock:
  815. mutex_unlock(&tty_mutex);
  816. }
  817. #endif
  818. #ifdef CONFIG_QUOTA_NETLINK_INTERFACE
  819. /* Netlink family structure for quota */
  820. static struct genl_family quota_genl_family = {
  821. .id = GENL_ID_GENERATE,
  822. .hdrsize = 0,
  823. .name = "VFS_DQUOT",
  824. .version = 1,
  825. .maxattr = QUOTA_NL_A_MAX,
  826. };
  827. /* Send warning to userspace about user which exceeded quota */
  828. static void send_warning(const struct dquot *dquot, const char warntype)
  829. {
  830. static atomic_t seq;
  831. struct sk_buff *skb;
  832. void *msg_head;
  833. int ret;
  834. int msg_size = 4 * nla_total_size(sizeof(u32)) +
  835. 2 * nla_total_size(sizeof(u64));
  836. /* We have to allocate using GFP_NOFS as we are called from a
  837. * filesystem performing write and thus further recursion into
  838. * the fs to free some data could cause deadlocks. */
  839. skb = genlmsg_new(msg_size, GFP_NOFS);
  840. if (!skb) {
  841. printk(KERN_ERR
  842. "VFS: Not enough memory to send quota warning.\n");
  843. return;
  844. }
  845. msg_head = genlmsg_put(skb, 0, atomic_add_return(1, &seq),
  846. &quota_genl_family, 0, QUOTA_NL_C_WARNING);
  847. if (!msg_head) {
  848. printk(KERN_ERR
  849. "VFS: Cannot store netlink header in quota warning.\n");
  850. goto err_out;
  851. }
  852. ret = nla_put_u32(skb, QUOTA_NL_A_QTYPE, dquot->dq_type);
  853. if (ret)
  854. goto attr_err_out;
  855. ret = nla_put_u64(skb, QUOTA_NL_A_EXCESS_ID, dquot->dq_id);
  856. if (ret)
  857. goto attr_err_out;
  858. ret = nla_put_u32(skb, QUOTA_NL_A_WARNING, warntype);
  859. if (ret)
  860. goto attr_err_out;
  861. ret = nla_put_u32(skb, QUOTA_NL_A_DEV_MAJOR,
  862. MAJOR(dquot->dq_sb->s_dev));
  863. if (ret)
  864. goto attr_err_out;
  865. ret = nla_put_u32(skb, QUOTA_NL_A_DEV_MINOR,
  866. MINOR(dquot->dq_sb->s_dev));
  867. if (ret)
  868. goto attr_err_out;
  869. ret = nla_put_u64(skb, QUOTA_NL_A_CAUSED_ID, current->user->uid);
  870. if (ret)
  871. goto attr_err_out;
  872. genlmsg_end(skb, msg_head);
  873. ret = genlmsg_multicast(skb, 0, quota_genl_family.id, GFP_NOFS);
  874. if (ret < 0 && ret != -ESRCH)
  875. printk(KERN_ERR
  876. "VFS: Failed to send notification message: %d\n", ret);
  877. return;
  878. attr_err_out:
  879. printk(KERN_ERR "VFS: Not enough space to compose quota message!\n");
  880. err_out:
  881. kfree_skb(skb);
  882. }
  883. #endif
  884. static inline void flush_warnings(struct dquot * const *dquots, char *warntype)
  885. {
  886. int i;
  887. for (i = 0; i < MAXQUOTAS; i++)
  888. if (dquots[i] != NODQUOT && warntype[i] != QUOTA_NL_NOWARN &&
  889. !warning_issued(dquots[i], warntype[i])) {
  890. #ifdef CONFIG_PRINT_QUOTA_WARNING
  891. print_warning(dquots[i], warntype[i]);
  892. #endif
  893. #ifdef CONFIG_QUOTA_NETLINK_INTERFACE
  894. send_warning(dquots[i], warntype[i]);
  895. #endif
  896. }
  897. }
  898. static inline char ignore_hardlimit(struct dquot *dquot)
  899. {
  900. struct mem_dqinfo *info = &sb_dqopt(dquot->dq_sb)->info[dquot->dq_type];
  901. return capable(CAP_SYS_RESOURCE) &&
  902. (info->dqi_format->qf_fmt_id != QFMT_VFS_OLD || !(info->dqi_flags & V1_DQF_RSQUASH));
  903. }
  904. /* needs dq_data_lock */
  905. static int check_idq(struct dquot *dquot, ulong inodes, char *warntype)
  906. {
  907. *warntype = QUOTA_NL_NOWARN;
  908. if (inodes <= 0 || test_bit(DQ_FAKE_B, &dquot->dq_flags))
  909. return QUOTA_OK;
  910. if (dquot->dq_dqb.dqb_ihardlimit &&
  911. (dquot->dq_dqb.dqb_curinodes + inodes) > dquot->dq_dqb.dqb_ihardlimit &&
  912. !ignore_hardlimit(dquot)) {
  913. *warntype = QUOTA_NL_IHARDWARN;
  914. return NO_QUOTA;
  915. }
  916. if (dquot->dq_dqb.dqb_isoftlimit &&
  917. (dquot->dq_dqb.dqb_curinodes + inodes) > dquot->dq_dqb.dqb_isoftlimit &&
  918. dquot->dq_dqb.dqb_itime && get_seconds() >= dquot->dq_dqb.dqb_itime &&
  919. !ignore_hardlimit(dquot)) {
  920. *warntype = QUOTA_NL_ISOFTLONGWARN;
  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 == 0) {
  926. *warntype = QUOTA_NL_ISOFTWARN;
  927. dquot->dq_dqb.dqb_itime = get_seconds() + sb_dqopt(dquot->dq_sb)->info[dquot->dq_type].dqi_igrace;
  928. }
  929. return QUOTA_OK;
  930. }
  931. /* needs dq_data_lock */
  932. static int check_bdq(struct dquot *dquot, qsize_t space, int prealloc, char *warntype)
  933. {
  934. *warntype = QUOTA_NL_NOWARN;
  935. if (space <= 0 || test_bit(DQ_FAKE_B, &dquot->dq_flags))
  936. return QUOTA_OK;
  937. if (dquot->dq_dqb.dqb_bhardlimit &&
  938. toqb(dquot->dq_dqb.dqb_curspace + space) > dquot->dq_dqb.dqb_bhardlimit &&
  939. !ignore_hardlimit(dquot)) {
  940. if (!prealloc)
  941. *warntype = QUOTA_NL_BHARDWARN;
  942. return NO_QUOTA;
  943. }
  944. if (dquot->dq_dqb.dqb_bsoftlimit &&
  945. toqb(dquot->dq_dqb.dqb_curspace + space) > dquot->dq_dqb.dqb_bsoftlimit &&
  946. dquot->dq_dqb.dqb_btime && get_seconds() >= dquot->dq_dqb.dqb_btime &&
  947. !ignore_hardlimit(dquot)) {
  948. if (!prealloc)
  949. *warntype = QUOTA_NL_BSOFTLONGWARN;
  950. return NO_QUOTA;
  951. }
  952. if (dquot->dq_dqb.dqb_bsoftlimit &&
  953. toqb(dquot->dq_dqb.dqb_curspace + space) > dquot->dq_dqb.dqb_bsoftlimit &&
  954. dquot->dq_dqb.dqb_btime == 0) {
  955. if (!prealloc) {
  956. *warntype = QUOTA_NL_BSOFTWARN;
  957. dquot->dq_dqb.dqb_btime = get_seconds() + sb_dqopt(dquot->dq_sb)->info[dquot->dq_type].dqi_bgrace;
  958. }
  959. else
  960. /*
  961. * We don't allow preallocation to exceed softlimit so exceeding will
  962. * be always printed
  963. */
  964. return NO_QUOTA;
  965. }
  966. return QUOTA_OK;
  967. }
  968. /*
  969. * Initialize quota pointers in inode
  970. * Transaction must be started at entry
  971. */
  972. int dquot_initialize(struct inode *inode, int type)
  973. {
  974. unsigned int id = 0;
  975. int cnt, ret = 0;
  976. /* First test before acquiring mutex - solves deadlocks when we
  977. * re-enter the quota code and are already holding the mutex */
  978. if (IS_NOQUOTA(inode))
  979. return 0;
  980. down_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
  981. /* Having dqptr_sem we know NOQUOTA flags can't be altered... */
  982. if (IS_NOQUOTA(inode))
  983. goto out_err;
  984. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  985. if (type != -1 && cnt != type)
  986. continue;
  987. if (inode->i_dquot[cnt] == NODQUOT) {
  988. switch (cnt) {
  989. case USRQUOTA:
  990. id = inode->i_uid;
  991. break;
  992. case GRPQUOTA:
  993. id = inode->i_gid;
  994. break;
  995. }
  996. inode->i_dquot[cnt] = dqget(inode->i_sb, id, cnt);
  997. }
  998. }
  999. out_err:
  1000. up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1001. return ret;
  1002. }
  1003. /*
  1004. * Release all quotas referenced by inode
  1005. * Transaction must be started at an entry
  1006. */
  1007. int dquot_drop(struct inode *inode)
  1008. {
  1009. int cnt;
  1010. down_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1011. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1012. if (inode->i_dquot[cnt] != NODQUOT) {
  1013. dqput(inode->i_dquot[cnt]);
  1014. inode->i_dquot[cnt] = NODQUOT;
  1015. }
  1016. }
  1017. up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1018. return 0;
  1019. }
  1020. /*
  1021. * Following four functions update i_blocks+i_bytes fields and
  1022. * quota information (together with appropriate checks)
  1023. * NOTE: We absolutely rely on the fact that caller dirties
  1024. * the inode (usually macros in quotaops.h care about this) and
  1025. * holds a handle for the current transaction so that dquot write and
  1026. * inode write go into the same transaction.
  1027. */
  1028. /*
  1029. * This operation can block, but only after everything is updated
  1030. */
  1031. int dquot_alloc_space(struct inode *inode, qsize_t number, int warn)
  1032. {
  1033. int cnt, ret = NO_QUOTA;
  1034. char warntype[MAXQUOTAS];
  1035. /* First test before acquiring mutex - solves deadlocks when we
  1036. * re-enter the quota code and are already holding the mutex */
  1037. if (IS_NOQUOTA(inode)) {
  1038. out_add:
  1039. inode_add_bytes(inode, number);
  1040. return QUOTA_OK;
  1041. }
  1042. for (cnt = 0; cnt < MAXQUOTAS; cnt++)
  1043. warntype[cnt] = QUOTA_NL_NOWARN;
  1044. down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1045. if (IS_NOQUOTA(inode)) { /* Now we can do reliable test... */
  1046. up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1047. goto out_add;
  1048. }
  1049. spin_lock(&dq_data_lock);
  1050. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1051. if (inode->i_dquot[cnt] == NODQUOT)
  1052. continue;
  1053. if (check_bdq(inode->i_dquot[cnt], number, warn, warntype+cnt) == NO_QUOTA)
  1054. goto warn_put_all;
  1055. }
  1056. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1057. if (inode->i_dquot[cnt] == NODQUOT)
  1058. continue;
  1059. dquot_incr_space(inode->i_dquot[cnt], number);
  1060. }
  1061. inode_add_bytes(inode, number);
  1062. ret = QUOTA_OK;
  1063. warn_put_all:
  1064. spin_unlock(&dq_data_lock);
  1065. if (ret == QUOTA_OK)
  1066. /* Dirtify all the dquots - this can block when journalling */
  1067. for (cnt = 0; cnt < MAXQUOTAS; cnt++)
  1068. if (inode->i_dquot[cnt])
  1069. mark_dquot_dirty(inode->i_dquot[cnt]);
  1070. flush_warnings(inode->i_dquot, warntype);
  1071. up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1072. return ret;
  1073. }
  1074. /*
  1075. * This operation can block, but only after everything is updated
  1076. */
  1077. int dquot_alloc_inode(const struct inode *inode, unsigned long number)
  1078. {
  1079. int cnt, ret = NO_QUOTA;
  1080. char warntype[MAXQUOTAS];
  1081. /* First test before acquiring mutex - solves deadlocks when we
  1082. * re-enter the quota code and are already holding the mutex */
  1083. if (IS_NOQUOTA(inode))
  1084. return QUOTA_OK;
  1085. for (cnt = 0; cnt < MAXQUOTAS; cnt++)
  1086. warntype[cnt] = QUOTA_NL_NOWARN;
  1087. down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1088. if (IS_NOQUOTA(inode)) {
  1089. up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1090. return QUOTA_OK;
  1091. }
  1092. spin_lock(&dq_data_lock);
  1093. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1094. if (inode->i_dquot[cnt] == NODQUOT)
  1095. continue;
  1096. if (check_idq(inode->i_dquot[cnt], number, warntype+cnt) == NO_QUOTA)
  1097. goto warn_put_all;
  1098. }
  1099. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1100. if (inode->i_dquot[cnt] == NODQUOT)
  1101. continue;
  1102. dquot_incr_inodes(inode->i_dquot[cnt], number);
  1103. }
  1104. ret = QUOTA_OK;
  1105. warn_put_all:
  1106. spin_unlock(&dq_data_lock);
  1107. if (ret == QUOTA_OK)
  1108. /* Dirtify all the dquots - this can block when journalling */
  1109. for (cnt = 0; cnt < MAXQUOTAS; cnt++)
  1110. if (inode->i_dquot[cnt])
  1111. mark_dquot_dirty(inode->i_dquot[cnt]);
  1112. flush_warnings(inode->i_dquot, warntype);
  1113. up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1114. return ret;
  1115. }
  1116. /*
  1117. * This operation can block, but only after everything is updated
  1118. */
  1119. int dquot_free_space(struct inode *inode, qsize_t number)
  1120. {
  1121. unsigned int cnt;
  1122. /* First test before acquiring mutex - solves deadlocks when we
  1123. * re-enter the quota code and are already holding the mutex */
  1124. if (IS_NOQUOTA(inode)) {
  1125. out_sub:
  1126. inode_sub_bytes(inode, number);
  1127. return QUOTA_OK;
  1128. }
  1129. down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1130. /* Now recheck reliably when holding dqptr_sem */
  1131. if (IS_NOQUOTA(inode)) {
  1132. up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1133. goto out_sub;
  1134. }
  1135. spin_lock(&dq_data_lock);
  1136. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1137. if (inode->i_dquot[cnt] == NODQUOT)
  1138. continue;
  1139. dquot_decr_space(inode->i_dquot[cnt], number);
  1140. }
  1141. inode_sub_bytes(inode, number);
  1142. spin_unlock(&dq_data_lock);
  1143. /* Dirtify all the dquots - this can block when journalling */
  1144. for (cnt = 0; cnt < MAXQUOTAS; cnt++)
  1145. if (inode->i_dquot[cnt])
  1146. mark_dquot_dirty(inode->i_dquot[cnt]);
  1147. up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1148. return QUOTA_OK;
  1149. }
  1150. /*
  1151. * This operation can block, but only after everything is updated
  1152. */
  1153. int dquot_free_inode(const struct inode *inode, unsigned long number)
  1154. {
  1155. unsigned int cnt;
  1156. /* First test before acquiring mutex - solves deadlocks when we
  1157. * re-enter the quota code and are already holding the mutex */
  1158. if (IS_NOQUOTA(inode))
  1159. return QUOTA_OK;
  1160. down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1161. /* Now recheck reliably when holding dqptr_sem */
  1162. if (IS_NOQUOTA(inode)) {
  1163. up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1164. return QUOTA_OK;
  1165. }
  1166. spin_lock(&dq_data_lock);
  1167. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1168. if (inode->i_dquot[cnt] == NODQUOT)
  1169. continue;
  1170. dquot_decr_inodes(inode->i_dquot[cnt], number);
  1171. }
  1172. spin_unlock(&dq_data_lock);
  1173. /* Dirtify all the dquots - this can block when journalling */
  1174. for (cnt = 0; cnt < MAXQUOTAS; cnt++)
  1175. if (inode->i_dquot[cnt])
  1176. mark_dquot_dirty(inode->i_dquot[cnt]);
  1177. up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1178. return QUOTA_OK;
  1179. }
  1180. /*
  1181. * Transfer the number of inode and blocks from one diskquota to an other.
  1182. *
  1183. * This operation can block, but only after everything is updated
  1184. * A transaction must be started when entering this function.
  1185. */
  1186. int dquot_transfer(struct inode *inode, struct iattr *iattr)
  1187. {
  1188. qsize_t space;
  1189. struct dquot *transfer_from[MAXQUOTAS];
  1190. struct dquot *transfer_to[MAXQUOTAS];
  1191. int cnt, ret = NO_QUOTA, chuid = (iattr->ia_valid & ATTR_UID) && inode->i_uid != iattr->ia_uid,
  1192. chgid = (iattr->ia_valid & ATTR_GID) && inode->i_gid != iattr->ia_gid;
  1193. char warntype[MAXQUOTAS];
  1194. /* First test before acquiring mutex - solves deadlocks when we
  1195. * re-enter the quota code and are already holding the mutex */
  1196. if (IS_NOQUOTA(inode))
  1197. return QUOTA_OK;
  1198. /* Clear the arrays */
  1199. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1200. transfer_to[cnt] = transfer_from[cnt] = NODQUOT;
  1201. warntype[cnt] = QUOTA_NL_NOWARN;
  1202. }
  1203. down_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1204. /* Now recheck reliably when holding dqptr_sem */
  1205. if (IS_NOQUOTA(inode)) { /* File without quota accounting? */
  1206. up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1207. return QUOTA_OK;
  1208. }
  1209. /* First build the transfer_to list - here we can block on
  1210. * reading/instantiating of dquots. We know that the transaction for
  1211. * us was already started so we don't violate lock ranking here */
  1212. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1213. switch (cnt) {
  1214. case USRQUOTA:
  1215. if (!chuid)
  1216. continue;
  1217. transfer_to[cnt] = dqget(inode->i_sb, iattr->ia_uid, cnt);
  1218. break;
  1219. case GRPQUOTA:
  1220. if (!chgid)
  1221. continue;
  1222. transfer_to[cnt] = dqget(inode->i_sb, iattr->ia_gid, cnt);
  1223. break;
  1224. }
  1225. }
  1226. spin_lock(&dq_data_lock);
  1227. space = inode_get_bytes(inode);
  1228. /* Build the transfer_from list and check the limits */
  1229. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1230. if (transfer_to[cnt] == NODQUOT)
  1231. continue;
  1232. transfer_from[cnt] = inode->i_dquot[cnt];
  1233. if (check_idq(transfer_to[cnt], 1, warntype+cnt) == NO_QUOTA ||
  1234. check_bdq(transfer_to[cnt], space, 0, warntype+cnt) == NO_QUOTA)
  1235. goto warn_put_all;
  1236. }
  1237. /*
  1238. * Finally perform the needed transfer from transfer_from to transfer_to
  1239. */
  1240. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1241. /*
  1242. * Skip changes for same uid or gid or for turned off quota-type.
  1243. */
  1244. if (transfer_to[cnt] == NODQUOT)
  1245. continue;
  1246. /* Due to IO error we might not have transfer_from[] structure */
  1247. if (transfer_from[cnt]) {
  1248. dquot_decr_inodes(transfer_from[cnt], 1);
  1249. dquot_decr_space(transfer_from[cnt], space);
  1250. }
  1251. dquot_incr_inodes(transfer_to[cnt], 1);
  1252. dquot_incr_space(transfer_to[cnt], space);
  1253. inode->i_dquot[cnt] = transfer_to[cnt];
  1254. }
  1255. ret = QUOTA_OK;
  1256. warn_put_all:
  1257. spin_unlock(&dq_data_lock);
  1258. /* Dirtify all the dquots - this can block when journalling */
  1259. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1260. if (transfer_from[cnt])
  1261. mark_dquot_dirty(transfer_from[cnt]);
  1262. if (transfer_to[cnt])
  1263. mark_dquot_dirty(transfer_to[cnt]);
  1264. }
  1265. flush_warnings(transfer_to, warntype);
  1266. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1267. if (ret == QUOTA_OK && transfer_from[cnt] != NODQUOT)
  1268. dqput(transfer_from[cnt]);
  1269. if (ret == NO_QUOTA && transfer_to[cnt] != NODQUOT)
  1270. dqput(transfer_to[cnt]);
  1271. }
  1272. up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1273. return ret;
  1274. }
  1275. /*
  1276. * Write info of quota file to disk
  1277. */
  1278. int dquot_commit_info(struct super_block *sb, int type)
  1279. {
  1280. int ret;
  1281. struct quota_info *dqopt = sb_dqopt(sb);
  1282. mutex_lock(&dqopt->dqio_mutex);
  1283. ret = dqopt->ops[type]->write_file_info(sb, type);
  1284. mutex_unlock(&dqopt->dqio_mutex);
  1285. return ret;
  1286. }
  1287. /*
  1288. * Definitions of diskquota operations.
  1289. */
  1290. struct dquot_operations dquot_operations = {
  1291. .initialize = dquot_initialize,
  1292. .drop = dquot_drop,
  1293. .alloc_space = dquot_alloc_space,
  1294. .alloc_inode = dquot_alloc_inode,
  1295. .free_space = dquot_free_space,
  1296. .free_inode = dquot_free_inode,
  1297. .transfer = dquot_transfer,
  1298. .write_dquot = dquot_commit,
  1299. .acquire_dquot = dquot_acquire,
  1300. .release_dquot = dquot_release,
  1301. .mark_dirty = dquot_mark_dquot_dirty,
  1302. .write_info = dquot_commit_info
  1303. };
  1304. static inline void set_enable_flags(struct quota_info *dqopt, int type)
  1305. {
  1306. switch (type) {
  1307. case USRQUOTA:
  1308. dqopt->flags |= DQUOT_USR_ENABLED;
  1309. break;
  1310. case GRPQUOTA:
  1311. dqopt->flags |= DQUOT_GRP_ENABLED;
  1312. break;
  1313. }
  1314. }
  1315. static inline void reset_enable_flags(struct quota_info *dqopt, int type)
  1316. {
  1317. switch (type) {
  1318. case USRQUOTA:
  1319. dqopt->flags &= ~DQUOT_USR_ENABLED;
  1320. break;
  1321. case GRPQUOTA:
  1322. dqopt->flags &= ~DQUOT_GRP_ENABLED;
  1323. break;
  1324. }
  1325. }
  1326. /*
  1327. * Turn quota off on a device. type == -1 ==> quotaoff for all types (umount)
  1328. */
  1329. int vfs_quota_off(struct super_block *sb, int type)
  1330. {
  1331. int cnt;
  1332. struct quota_info *dqopt = sb_dqopt(sb);
  1333. struct inode *toputinode[MAXQUOTAS];
  1334. /* We need to serialize quota_off() for device */
  1335. mutex_lock(&dqopt->dqonoff_mutex);
  1336. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1337. toputinode[cnt] = NULL;
  1338. if (type != -1 && cnt != type)
  1339. continue;
  1340. if (!sb_has_quota_enabled(sb, cnt))
  1341. continue;
  1342. reset_enable_flags(dqopt, cnt);
  1343. /* Note: these are blocking operations */
  1344. drop_dquot_ref(sb, cnt);
  1345. invalidate_dquots(sb, cnt);
  1346. /*
  1347. * Now all dquots should be invalidated, all writes done so we should be only
  1348. * users of the info. No locks needed.
  1349. */
  1350. if (info_dirty(&dqopt->info[cnt]))
  1351. sb->dq_op->write_info(sb, cnt);
  1352. if (dqopt->ops[cnt]->free_file_info)
  1353. dqopt->ops[cnt]->free_file_info(sb, cnt);
  1354. put_quota_format(dqopt->info[cnt].dqi_format);
  1355. toputinode[cnt] = dqopt->files[cnt];
  1356. dqopt->files[cnt] = NULL;
  1357. dqopt->info[cnt].dqi_flags = 0;
  1358. dqopt->info[cnt].dqi_igrace = 0;
  1359. dqopt->info[cnt].dqi_bgrace = 0;
  1360. dqopt->ops[cnt] = NULL;
  1361. }
  1362. mutex_unlock(&dqopt->dqonoff_mutex);
  1363. /* Sync the superblock so that buffers with quota data are written to
  1364. * disk (and so userspace sees correct data afterwards). */
  1365. if (sb->s_op->sync_fs)
  1366. sb->s_op->sync_fs(sb, 1);
  1367. sync_blockdev(sb->s_bdev);
  1368. /* Now the quota files are just ordinary files and we can set the
  1369. * inode flags back. Moreover we discard the pagecache so that
  1370. * userspace sees the writes we did bypassing the pagecache. We
  1371. * must also discard the blockdev buffers so that we see the
  1372. * changes done by userspace on the next quotaon() */
  1373. for (cnt = 0; cnt < MAXQUOTAS; cnt++)
  1374. if (toputinode[cnt]) {
  1375. mutex_lock(&dqopt->dqonoff_mutex);
  1376. /* If quota was reenabled in the meantime, we have
  1377. * nothing to do */
  1378. if (!sb_has_quota_enabled(sb, cnt)) {
  1379. mutex_lock_nested(&toputinode[cnt]->i_mutex, I_MUTEX_QUOTA);
  1380. toputinode[cnt]->i_flags &= ~(S_IMMUTABLE |
  1381. S_NOATIME | S_NOQUOTA);
  1382. truncate_inode_pages(&toputinode[cnt]->i_data, 0);
  1383. mutex_unlock(&toputinode[cnt]->i_mutex);
  1384. mark_inode_dirty(toputinode[cnt]);
  1385. iput(toputinode[cnt]);
  1386. }
  1387. mutex_unlock(&dqopt->dqonoff_mutex);
  1388. }
  1389. if (sb->s_bdev)
  1390. invalidate_bdev(sb->s_bdev);
  1391. return 0;
  1392. }
  1393. /*
  1394. * Turn quotas on on a device
  1395. */
  1396. /* Helper function when we already have the inode */
  1397. static int vfs_quota_on_inode(struct inode *inode, int type, int format_id)
  1398. {
  1399. struct quota_format_type *fmt = find_quota_format(format_id);
  1400. struct super_block *sb = inode->i_sb;
  1401. struct quota_info *dqopt = sb_dqopt(sb);
  1402. int error;
  1403. int oldflags = -1;
  1404. if (!fmt)
  1405. return -ESRCH;
  1406. if (!S_ISREG(inode->i_mode)) {
  1407. error = -EACCES;
  1408. goto out_fmt;
  1409. }
  1410. if (IS_RDONLY(inode)) {
  1411. error = -EROFS;
  1412. goto out_fmt;
  1413. }
  1414. if (!sb->s_op->quota_write || !sb->s_op->quota_read) {
  1415. error = -EINVAL;
  1416. goto out_fmt;
  1417. }
  1418. /* As we bypass the pagecache we must now flush the inode so that
  1419. * we see all the changes from userspace... */
  1420. write_inode_now(inode, 1);
  1421. /* And now flush the block cache so that kernel sees the changes */
  1422. invalidate_bdev(sb->s_bdev);
  1423. mutex_lock(&inode->i_mutex);
  1424. mutex_lock(&dqopt->dqonoff_mutex);
  1425. if (sb_has_quota_enabled(sb, type)) {
  1426. error = -EBUSY;
  1427. goto out_lock;
  1428. }
  1429. /* We don't want quota and atime on quota files (deadlocks possible)
  1430. * Also nobody should write to the file - we use special IO operations
  1431. * which ignore the immutable bit. */
  1432. down_write(&dqopt->dqptr_sem);
  1433. oldflags = inode->i_flags & (S_NOATIME | S_IMMUTABLE | S_NOQUOTA);
  1434. inode->i_flags |= S_NOQUOTA | S_NOATIME | S_IMMUTABLE;
  1435. up_write(&dqopt->dqptr_sem);
  1436. sb->dq_op->drop(inode);
  1437. error = -EIO;
  1438. dqopt->files[type] = igrab(inode);
  1439. if (!dqopt->files[type])
  1440. goto out_lock;
  1441. error = -EINVAL;
  1442. if (!fmt->qf_ops->check_quota_file(sb, type))
  1443. goto out_file_init;
  1444. dqopt->ops[type] = fmt->qf_ops;
  1445. dqopt->info[type].dqi_format = fmt;
  1446. INIT_LIST_HEAD(&dqopt->info[type].dqi_dirty_list);
  1447. mutex_lock(&dqopt->dqio_mutex);
  1448. if ((error = dqopt->ops[type]->read_file_info(sb, type)) < 0) {
  1449. mutex_unlock(&dqopt->dqio_mutex);
  1450. goto out_file_init;
  1451. }
  1452. mutex_unlock(&dqopt->dqio_mutex);
  1453. mutex_unlock(&inode->i_mutex);
  1454. set_enable_flags(dqopt, type);
  1455. add_dquot_ref(sb, type);
  1456. mutex_unlock(&dqopt->dqonoff_mutex);
  1457. return 0;
  1458. out_file_init:
  1459. dqopt->files[type] = NULL;
  1460. iput(inode);
  1461. out_lock:
  1462. mutex_unlock(&dqopt->dqonoff_mutex);
  1463. if (oldflags != -1) {
  1464. down_write(&dqopt->dqptr_sem);
  1465. /* Set the flags back (in the case of accidental quotaon()
  1466. * on a wrong file we don't want to mess up the flags) */
  1467. inode->i_flags &= ~(S_NOATIME | S_NOQUOTA | S_IMMUTABLE);
  1468. inode->i_flags |= oldflags;
  1469. up_write(&dqopt->dqptr_sem);
  1470. }
  1471. mutex_unlock(&inode->i_mutex);
  1472. out_fmt:
  1473. put_quota_format(fmt);
  1474. return error;
  1475. }
  1476. /* Actual function called from quotactl() */
  1477. int vfs_quota_on(struct super_block *sb, int type, int format_id, char *path)
  1478. {
  1479. struct nameidata nd;
  1480. int error;
  1481. error = path_lookup(path, LOOKUP_FOLLOW, &nd);
  1482. if (error < 0)
  1483. return error;
  1484. error = security_quota_on(nd.dentry);
  1485. if (error)
  1486. goto out_path;
  1487. /* Quota file not on the same filesystem? */
  1488. if (nd.mnt->mnt_sb != sb)
  1489. error = -EXDEV;
  1490. else
  1491. error = vfs_quota_on_inode(nd.dentry->d_inode, type, format_id);
  1492. out_path:
  1493. path_release(&nd);
  1494. return error;
  1495. }
  1496. /*
  1497. * This function is used when filesystem needs to initialize quotas
  1498. * during mount time.
  1499. */
  1500. int vfs_quota_on_mount(struct super_block *sb, char *qf_name,
  1501. int format_id, int type)
  1502. {
  1503. struct dentry *dentry;
  1504. int error;
  1505. dentry = lookup_one_len(qf_name, sb->s_root, strlen(qf_name));
  1506. if (IS_ERR(dentry))
  1507. return PTR_ERR(dentry);
  1508. if (!dentry->d_inode) {
  1509. error = -ENOENT;
  1510. goto out;
  1511. }
  1512. error = security_quota_on(dentry);
  1513. if (!error)
  1514. error = vfs_quota_on_inode(dentry->d_inode, type, format_id);
  1515. out:
  1516. dput(dentry);
  1517. return error;
  1518. }
  1519. /* Generic routine for getting common part of quota structure */
  1520. static void do_get_dqblk(struct dquot *dquot, struct if_dqblk *di)
  1521. {
  1522. struct mem_dqblk *dm = &dquot->dq_dqb;
  1523. spin_lock(&dq_data_lock);
  1524. di->dqb_bhardlimit = dm->dqb_bhardlimit;
  1525. di->dqb_bsoftlimit = dm->dqb_bsoftlimit;
  1526. di->dqb_curspace = dm->dqb_curspace;
  1527. di->dqb_ihardlimit = dm->dqb_ihardlimit;
  1528. di->dqb_isoftlimit = dm->dqb_isoftlimit;
  1529. di->dqb_curinodes = dm->dqb_curinodes;
  1530. di->dqb_btime = dm->dqb_btime;
  1531. di->dqb_itime = dm->dqb_itime;
  1532. di->dqb_valid = QIF_ALL;
  1533. spin_unlock(&dq_data_lock);
  1534. }
  1535. int vfs_get_dqblk(struct super_block *sb, int type, qid_t id, struct if_dqblk *di)
  1536. {
  1537. struct dquot *dquot;
  1538. mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
  1539. if (!(dquot = dqget(sb, id, type))) {
  1540. mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
  1541. return -ESRCH;
  1542. }
  1543. do_get_dqblk(dquot, di);
  1544. dqput(dquot);
  1545. mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
  1546. return 0;
  1547. }
  1548. /* Generic routine for setting common part of quota structure */
  1549. static void do_set_dqblk(struct dquot *dquot, struct if_dqblk *di)
  1550. {
  1551. struct mem_dqblk *dm = &dquot->dq_dqb;
  1552. int check_blim = 0, check_ilim = 0;
  1553. spin_lock(&dq_data_lock);
  1554. if (di->dqb_valid & QIF_SPACE) {
  1555. dm->dqb_curspace = di->dqb_curspace;
  1556. check_blim = 1;
  1557. }
  1558. if (di->dqb_valid & QIF_BLIMITS) {
  1559. dm->dqb_bsoftlimit = di->dqb_bsoftlimit;
  1560. dm->dqb_bhardlimit = di->dqb_bhardlimit;
  1561. check_blim = 1;
  1562. }
  1563. if (di->dqb_valid & QIF_INODES) {
  1564. dm->dqb_curinodes = di->dqb_curinodes;
  1565. check_ilim = 1;
  1566. }
  1567. if (di->dqb_valid & QIF_ILIMITS) {
  1568. dm->dqb_isoftlimit = di->dqb_isoftlimit;
  1569. dm->dqb_ihardlimit = di->dqb_ihardlimit;
  1570. check_ilim = 1;
  1571. }
  1572. if (di->dqb_valid & QIF_BTIME)
  1573. dm->dqb_btime = di->dqb_btime;
  1574. if (di->dqb_valid & QIF_ITIME)
  1575. dm->dqb_itime = di->dqb_itime;
  1576. if (check_blim) {
  1577. if (!dm->dqb_bsoftlimit || toqb(dm->dqb_curspace) < dm->dqb_bsoftlimit) {
  1578. dm->dqb_btime = 0;
  1579. clear_bit(DQ_BLKS_B, &dquot->dq_flags);
  1580. }
  1581. else if (!(di->dqb_valid & QIF_BTIME)) /* Set grace only if user hasn't provided his own... */
  1582. dm->dqb_btime = get_seconds() + sb_dqopt(dquot->dq_sb)->info[dquot->dq_type].dqi_bgrace;
  1583. }
  1584. if (check_ilim) {
  1585. if (!dm->dqb_isoftlimit || dm->dqb_curinodes < dm->dqb_isoftlimit) {
  1586. dm->dqb_itime = 0;
  1587. clear_bit(DQ_INODES_B, &dquot->dq_flags);
  1588. }
  1589. else if (!(di->dqb_valid & QIF_ITIME)) /* Set grace only if user hasn't provided his own... */
  1590. dm->dqb_itime = get_seconds() + sb_dqopt(dquot->dq_sb)->info[dquot->dq_type].dqi_igrace;
  1591. }
  1592. if (dm->dqb_bhardlimit || dm->dqb_bsoftlimit || dm->dqb_ihardlimit || dm->dqb_isoftlimit)
  1593. clear_bit(DQ_FAKE_B, &dquot->dq_flags);
  1594. else
  1595. set_bit(DQ_FAKE_B, &dquot->dq_flags);
  1596. spin_unlock(&dq_data_lock);
  1597. mark_dquot_dirty(dquot);
  1598. }
  1599. int vfs_set_dqblk(struct super_block *sb, int type, qid_t id, struct if_dqblk *di)
  1600. {
  1601. struct dquot *dquot;
  1602. mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
  1603. if (!(dquot = dqget(sb, id, type))) {
  1604. mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
  1605. return -ESRCH;
  1606. }
  1607. do_set_dqblk(dquot, di);
  1608. dqput(dquot);
  1609. mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
  1610. return 0;
  1611. }
  1612. /* Generic routine for getting common part of quota file information */
  1613. int vfs_get_dqinfo(struct super_block *sb, int type, struct if_dqinfo *ii)
  1614. {
  1615. struct mem_dqinfo *mi;
  1616. mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
  1617. if (!sb_has_quota_enabled(sb, type)) {
  1618. mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
  1619. return -ESRCH;
  1620. }
  1621. mi = sb_dqopt(sb)->info + type;
  1622. spin_lock(&dq_data_lock);
  1623. ii->dqi_bgrace = mi->dqi_bgrace;
  1624. ii->dqi_igrace = mi->dqi_igrace;
  1625. ii->dqi_flags = mi->dqi_flags & DQF_MASK;
  1626. ii->dqi_valid = IIF_ALL;
  1627. spin_unlock(&dq_data_lock);
  1628. mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
  1629. return 0;
  1630. }
  1631. /* Generic routine for setting common part of quota file information */
  1632. int vfs_set_dqinfo(struct super_block *sb, int type, struct if_dqinfo *ii)
  1633. {
  1634. struct mem_dqinfo *mi;
  1635. mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
  1636. if (!sb_has_quota_enabled(sb, type)) {
  1637. mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
  1638. return -ESRCH;
  1639. }
  1640. mi = sb_dqopt(sb)->info + type;
  1641. spin_lock(&dq_data_lock);
  1642. if (ii->dqi_valid & IIF_BGRACE)
  1643. mi->dqi_bgrace = ii->dqi_bgrace;
  1644. if (ii->dqi_valid & IIF_IGRACE)
  1645. mi->dqi_igrace = ii->dqi_igrace;
  1646. if (ii->dqi_valid & IIF_FLAGS)
  1647. mi->dqi_flags = (mi->dqi_flags & ~DQF_MASK) | (ii->dqi_flags & DQF_MASK);
  1648. spin_unlock(&dq_data_lock);
  1649. mark_info_dirty(sb, type);
  1650. /* Force write to disk */
  1651. sb->dq_op->write_info(sb, type);
  1652. mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
  1653. return 0;
  1654. }
  1655. struct quotactl_ops vfs_quotactl_ops = {
  1656. .quota_on = vfs_quota_on,
  1657. .quota_off = vfs_quota_off,
  1658. .quota_sync = vfs_quota_sync,
  1659. .get_info = vfs_get_dqinfo,
  1660. .set_info = vfs_set_dqinfo,
  1661. .get_dqblk = vfs_get_dqblk,
  1662. .set_dqblk = vfs_set_dqblk
  1663. };
  1664. static ctl_table fs_dqstats_table[] = {
  1665. {
  1666. .ctl_name = FS_DQ_LOOKUPS,
  1667. .procname = "lookups",
  1668. .data = &dqstats.lookups,
  1669. .maxlen = sizeof(int),
  1670. .mode = 0444,
  1671. .proc_handler = &proc_dointvec,
  1672. },
  1673. {
  1674. .ctl_name = FS_DQ_DROPS,
  1675. .procname = "drops",
  1676. .data = &dqstats.drops,
  1677. .maxlen = sizeof(int),
  1678. .mode = 0444,
  1679. .proc_handler = &proc_dointvec,
  1680. },
  1681. {
  1682. .ctl_name = FS_DQ_READS,
  1683. .procname = "reads",
  1684. .data = &dqstats.reads,
  1685. .maxlen = sizeof(int),
  1686. .mode = 0444,
  1687. .proc_handler = &proc_dointvec,
  1688. },
  1689. {
  1690. .ctl_name = FS_DQ_WRITES,
  1691. .procname = "writes",
  1692. .data = &dqstats.writes,
  1693. .maxlen = sizeof(int),
  1694. .mode = 0444,
  1695. .proc_handler = &proc_dointvec,
  1696. },
  1697. {
  1698. .ctl_name = FS_DQ_CACHE_HITS,
  1699. .procname = "cache_hits",
  1700. .data = &dqstats.cache_hits,
  1701. .maxlen = sizeof(int),
  1702. .mode = 0444,
  1703. .proc_handler = &proc_dointvec,
  1704. },
  1705. {
  1706. .ctl_name = FS_DQ_ALLOCATED,
  1707. .procname = "allocated_dquots",
  1708. .data = &dqstats.allocated_dquots,
  1709. .maxlen = sizeof(int),
  1710. .mode = 0444,
  1711. .proc_handler = &proc_dointvec,
  1712. },
  1713. {
  1714. .ctl_name = FS_DQ_FREE,
  1715. .procname = "free_dquots",
  1716. .data = &dqstats.free_dquots,
  1717. .maxlen = sizeof(int),
  1718. .mode = 0444,
  1719. .proc_handler = &proc_dointvec,
  1720. },
  1721. {
  1722. .ctl_name = FS_DQ_SYNCS,
  1723. .procname = "syncs",
  1724. .data = &dqstats.syncs,
  1725. .maxlen = sizeof(int),
  1726. .mode = 0444,
  1727. .proc_handler = &proc_dointvec,
  1728. },
  1729. #ifdef CONFIG_PRINT_QUOTA_WARNING
  1730. {
  1731. .ctl_name = FS_DQ_WARNINGS,
  1732. .procname = "warnings",
  1733. .data = &flag_print_warnings,
  1734. .maxlen = sizeof(int),
  1735. .mode = 0644,
  1736. .proc_handler = &proc_dointvec,
  1737. },
  1738. #endif
  1739. { .ctl_name = 0 },
  1740. };
  1741. static ctl_table fs_table[] = {
  1742. {
  1743. .ctl_name = FS_DQSTATS,
  1744. .procname = "quota",
  1745. .mode = 0555,
  1746. .child = fs_dqstats_table,
  1747. },
  1748. { .ctl_name = 0 },
  1749. };
  1750. static ctl_table sys_table[] = {
  1751. {
  1752. .ctl_name = CTL_FS,
  1753. .procname = "fs",
  1754. .mode = 0555,
  1755. .child = fs_table,
  1756. },
  1757. { .ctl_name = 0 },
  1758. };
  1759. static int __init dquot_init(void)
  1760. {
  1761. int i;
  1762. unsigned long nr_hash, order;
  1763. printk(KERN_NOTICE "VFS: Disk quotas %s\n", __DQUOT_VERSION__);
  1764. register_sysctl_table(sys_table);
  1765. dquot_cachep = kmem_cache_create("dquot",
  1766. sizeof(struct dquot), sizeof(unsigned long) * 4,
  1767. (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
  1768. SLAB_MEM_SPREAD|SLAB_PANIC),
  1769. NULL);
  1770. order = 0;
  1771. dquot_hash = (struct hlist_head *)__get_free_pages(GFP_ATOMIC, order);
  1772. if (!dquot_hash)
  1773. panic("Cannot create dquot hash table");
  1774. /* Find power-of-two hlist_heads which can fit into allocation */
  1775. nr_hash = (1UL << order) * PAGE_SIZE / sizeof(struct hlist_head);
  1776. dq_hash_bits = 0;
  1777. do {
  1778. dq_hash_bits++;
  1779. } while (nr_hash >> dq_hash_bits);
  1780. dq_hash_bits--;
  1781. nr_hash = 1UL << dq_hash_bits;
  1782. dq_hash_mask = nr_hash - 1;
  1783. for (i = 0; i < nr_hash; i++)
  1784. INIT_HLIST_HEAD(dquot_hash + i);
  1785. printk("Dquot-cache hash table entries: %ld (order %ld, %ld bytes)\n",
  1786. nr_hash, order, (PAGE_SIZE << order));
  1787. register_shrinker(&dqcache_shrinker);
  1788. #ifdef CONFIG_QUOTA_NETLINK_INTERFACE
  1789. if (genl_register_family(&quota_genl_family) != 0)
  1790. printk(KERN_ERR "VFS: Failed to create quota netlink interface.\n");
  1791. #endif
  1792. return 0;
  1793. }
  1794. module_init(dquot_init);
  1795. EXPORT_SYMBOL(register_quota_format);
  1796. EXPORT_SYMBOL(unregister_quota_format);
  1797. EXPORT_SYMBOL(dqstats);
  1798. EXPORT_SYMBOL(dq_data_lock);
  1799. EXPORT_SYMBOL(vfs_quota_on);
  1800. EXPORT_SYMBOL(vfs_quota_on_mount);
  1801. EXPORT_SYMBOL(vfs_quota_off);
  1802. EXPORT_SYMBOL(vfs_quota_sync);
  1803. EXPORT_SYMBOL(vfs_get_dqinfo);
  1804. EXPORT_SYMBOL(vfs_set_dqinfo);
  1805. EXPORT_SYMBOL(vfs_get_dqblk);
  1806. EXPORT_SYMBOL(vfs_set_dqblk);
  1807. EXPORT_SYMBOL(dquot_commit);
  1808. EXPORT_SYMBOL(dquot_commit_info);
  1809. EXPORT_SYMBOL(dquot_acquire);
  1810. EXPORT_SYMBOL(dquot_release);
  1811. EXPORT_SYMBOL(dquot_mark_dquot_dirty);
  1812. EXPORT_SYMBOL(dquot_initialize);
  1813. EXPORT_SYMBOL(dquot_drop);
  1814. EXPORT_SYMBOL(dquot_alloc_space);
  1815. EXPORT_SYMBOL(dquot_alloc_inode);
  1816. EXPORT_SYMBOL(dquot_free_space);
  1817. EXPORT_SYMBOL(dquot_free_inode);
  1818. EXPORT_SYMBOL(dquot_transfer);