dquot.c 53 KB

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