dquot.c 51 KB

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