dquot.c 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876
  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 kmem_cache_t *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_alloc(dquot_cachep, SLAB_NOFS);
  547. if(!dquot)
  548. return NODQUOT;
  549. memset((caddr_t)dquot, 0, sizeof(struct dquot));
  550. mutex_init(&dquot->dq_lock);
  551. INIT_LIST_HEAD(&dquot->dq_free);
  552. INIT_LIST_HEAD(&dquot->dq_inuse);
  553. INIT_HLIST_NODE(&dquot->dq_hash);
  554. INIT_LIST_HEAD(&dquot->dq_dirty);
  555. init_waitqueue_head(&dquot->dq_wait_unused);
  556. dquot->dq_sb = sb;
  557. dquot->dq_type = type;
  558. atomic_set(&dquot->dq_count, 1);
  559. return dquot;
  560. }
  561. /*
  562. * Get reference to dquot
  563. * MUST be called with either dqptr_sem or dqonoff_mutex held
  564. */
  565. static struct dquot *dqget(struct super_block *sb, unsigned int id, int type)
  566. {
  567. unsigned int hashent = hashfn(sb, id, type);
  568. struct dquot *dquot, *empty = NODQUOT;
  569. if (!sb_has_quota_enabled(sb, type))
  570. return NODQUOT;
  571. we_slept:
  572. spin_lock(&dq_list_lock);
  573. if ((dquot = find_dquot(hashent, sb, id, type)) == NODQUOT) {
  574. if (empty == NODQUOT) {
  575. spin_unlock(&dq_list_lock);
  576. if ((empty = get_empty_dquot(sb, type)) == NODQUOT)
  577. schedule(); /* Try to wait for a moment... */
  578. goto we_slept;
  579. }
  580. dquot = empty;
  581. dquot->dq_id = id;
  582. /* all dquots go on the inuse_list */
  583. put_inuse(dquot);
  584. /* hash it first so it can be found */
  585. insert_dquot_hash(dquot);
  586. dqstats.lookups++;
  587. spin_unlock(&dq_list_lock);
  588. } else {
  589. if (!atomic_read(&dquot->dq_count))
  590. remove_free_dquot(dquot);
  591. atomic_inc(&dquot->dq_count);
  592. dqstats.cache_hits++;
  593. dqstats.lookups++;
  594. spin_unlock(&dq_list_lock);
  595. if (empty)
  596. kmem_cache_free(dquot_cachep, empty);
  597. }
  598. /* Wait for dq_lock - after this we know that either dquot_release() is already
  599. * finished or it will be canceled due to dq_count > 1 test */
  600. wait_on_dquot(dquot);
  601. /* Read the dquot and instantiate it (everything done only if needed) */
  602. if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags) && sb->dq_op->acquire_dquot(dquot) < 0) {
  603. dqput(dquot);
  604. return NODQUOT;
  605. }
  606. #ifdef __DQUOT_PARANOIA
  607. BUG_ON(!dquot->dq_sb); /* Has somebody invalidated entry under us? */
  608. #endif
  609. return dquot;
  610. }
  611. static int dqinit_needed(struct inode *inode, int type)
  612. {
  613. int cnt;
  614. if (IS_NOQUOTA(inode))
  615. return 0;
  616. if (type != -1)
  617. return inode->i_dquot[type] == NODQUOT;
  618. for (cnt = 0; cnt < MAXQUOTAS; cnt++)
  619. if (inode->i_dquot[cnt] == NODQUOT)
  620. return 1;
  621. return 0;
  622. }
  623. /* This routine is guarded by dqonoff_mutex mutex */
  624. static void add_dquot_ref(struct super_block *sb, int type)
  625. {
  626. struct list_head *p;
  627. restart:
  628. file_list_lock();
  629. list_for_each(p, &sb->s_files) {
  630. struct file *filp = list_entry(p, struct file, f_u.fu_list);
  631. struct inode *inode = filp->f_dentry->d_inode;
  632. if (filp->f_mode & FMODE_WRITE && dqinit_needed(inode, type)) {
  633. struct dentry *dentry = dget(filp->f_dentry);
  634. file_list_unlock();
  635. sb->dq_op->initialize(inode, type);
  636. dput(dentry);
  637. /* As we may have blocked we had better restart... */
  638. goto restart;
  639. }
  640. }
  641. file_list_unlock();
  642. }
  643. /* Return 0 if dqput() won't block (note that 1 doesn't necessarily mean blocking) */
  644. static inline int dqput_blocks(struct dquot *dquot)
  645. {
  646. if (atomic_read(&dquot->dq_count) <= 1)
  647. return 1;
  648. return 0;
  649. }
  650. /* Remove references to dquots from inode - add dquot to list for freeing if needed */
  651. /* We can't race with anybody because we hold dqptr_sem for writing... */
  652. int remove_inode_dquot_ref(struct inode *inode, int type, struct list_head *tofree_head)
  653. {
  654. struct dquot *dquot = inode->i_dquot[type];
  655. inode->i_dquot[type] = NODQUOT;
  656. if (dquot != NODQUOT) {
  657. if (dqput_blocks(dquot)) {
  658. #ifdef __DQUOT_PARANOIA
  659. if (atomic_read(&dquot->dq_count) != 1)
  660. printk(KERN_WARNING "VFS: Adding dquot with dq_count %d to dispose list.\n", atomic_read(&dquot->dq_count));
  661. #endif
  662. spin_lock(&dq_list_lock);
  663. list_add(&dquot->dq_free, tofree_head); /* As dquot must have currently users it can't be on the free list... */
  664. spin_unlock(&dq_list_lock);
  665. return 1;
  666. }
  667. else
  668. dqput(dquot); /* We have guaranteed we won't block */
  669. }
  670. return 0;
  671. }
  672. /* Free list of dquots - called from inode.c */
  673. /* dquots are removed from inodes, no new references can be got so we are the only ones holding reference */
  674. static void put_dquot_list(struct list_head *tofree_head)
  675. {
  676. struct list_head *act_head;
  677. struct dquot *dquot;
  678. act_head = tofree_head->next;
  679. /* So now we have dquots on the list... Just free them */
  680. while (act_head != tofree_head) {
  681. dquot = list_entry(act_head, struct dquot, dq_free);
  682. act_head = act_head->next;
  683. list_del_init(&dquot->dq_free); /* Remove dquot from the list so we won't have problems... */
  684. dqput(dquot);
  685. }
  686. }
  687. /* Gather all references from inodes and drop them */
  688. static void drop_dquot_ref(struct super_block *sb, int type)
  689. {
  690. LIST_HEAD(tofree_head);
  691. down_write(&sb_dqopt(sb)->dqptr_sem);
  692. remove_dquot_ref(sb, type, &tofree_head);
  693. up_write(&sb_dqopt(sb)->dqptr_sem);
  694. put_dquot_list(&tofree_head);
  695. }
  696. static inline void dquot_incr_inodes(struct dquot *dquot, unsigned long number)
  697. {
  698. dquot->dq_dqb.dqb_curinodes += number;
  699. }
  700. static inline void dquot_incr_space(struct dquot *dquot, qsize_t number)
  701. {
  702. dquot->dq_dqb.dqb_curspace += number;
  703. }
  704. static inline void dquot_decr_inodes(struct dquot *dquot, unsigned long number)
  705. {
  706. if (dquot->dq_dqb.dqb_curinodes > number)
  707. dquot->dq_dqb.dqb_curinodes -= number;
  708. else
  709. dquot->dq_dqb.dqb_curinodes = 0;
  710. if (dquot->dq_dqb.dqb_curinodes <= dquot->dq_dqb.dqb_isoftlimit)
  711. dquot->dq_dqb.dqb_itime = (time_t) 0;
  712. clear_bit(DQ_INODES_B, &dquot->dq_flags);
  713. }
  714. static inline void dquot_decr_space(struct dquot *dquot, qsize_t number)
  715. {
  716. if (dquot->dq_dqb.dqb_curspace > number)
  717. dquot->dq_dqb.dqb_curspace -= number;
  718. else
  719. dquot->dq_dqb.dqb_curspace = 0;
  720. if (toqb(dquot->dq_dqb.dqb_curspace) <= dquot->dq_dqb.dqb_bsoftlimit)
  721. dquot->dq_dqb.dqb_btime = (time_t) 0;
  722. clear_bit(DQ_BLKS_B, &dquot->dq_flags);
  723. }
  724. static int flag_print_warnings = 1;
  725. static inline int need_print_warning(struct dquot *dquot)
  726. {
  727. if (!flag_print_warnings)
  728. return 0;
  729. switch (dquot->dq_type) {
  730. case USRQUOTA:
  731. return current->fsuid == dquot->dq_id;
  732. case GRPQUOTA:
  733. return in_group_p(dquot->dq_id);
  734. }
  735. return 0;
  736. }
  737. /* Values of warnings */
  738. #define NOWARN 0
  739. #define IHARDWARN 1
  740. #define ISOFTLONGWARN 2
  741. #define ISOFTWARN 3
  742. #define BHARDWARN 4
  743. #define BSOFTLONGWARN 5
  744. #define BSOFTWARN 6
  745. /* Print warning to user which exceeded quota */
  746. static void print_warning(struct dquot *dquot, const char warntype)
  747. {
  748. char *msg = NULL;
  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. tty_write_message(current->signal->tty, dquot->dq_sb->s_id);
  754. if (warntype == ISOFTWARN || warntype == BSOFTWARN)
  755. tty_write_message(current->signal->tty, ": warning, ");
  756. else
  757. tty_write_message(current->signal->tty, ": write failed, ");
  758. tty_write_message(current->signal->tty, quotatypes[dquot->dq_type]);
  759. switch (warntype) {
  760. case IHARDWARN:
  761. msg = " file limit reached.\r\n";
  762. break;
  763. case ISOFTLONGWARN:
  764. msg = " file quota exceeded too long.\r\n";
  765. break;
  766. case ISOFTWARN:
  767. msg = " file quota exceeded.\r\n";
  768. break;
  769. case BHARDWARN:
  770. msg = " block limit reached.\r\n";
  771. break;
  772. case BSOFTLONGWARN:
  773. msg = " block quota exceeded too long.\r\n";
  774. break;
  775. case BSOFTWARN:
  776. msg = " block quota exceeded.\r\n";
  777. break;
  778. }
  779. tty_write_message(current->signal->tty, msg);
  780. }
  781. static inline void flush_warnings(struct dquot **dquots, char *warntype)
  782. {
  783. int i;
  784. for (i = 0; i < MAXQUOTAS; i++)
  785. if (dquots[i] != NODQUOT && warntype[i] != NOWARN)
  786. print_warning(dquots[i], warntype[i]);
  787. }
  788. static inline char ignore_hardlimit(struct dquot *dquot)
  789. {
  790. struct mem_dqinfo *info = &sb_dqopt(dquot->dq_sb)->info[dquot->dq_type];
  791. return capable(CAP_SYS_RESOURCE) &&
  792. (info->dqi_format->qf_fmt_id != QFMT_VFS_OLD || !(info->dqi_flags & V1_DQF_RSQUASH));
  793. }
  794. /* needs dq_data_lock */
  795. static int check_idq(struct dquot *dquot, ulong inodes, char *warntype)
  796. {
  797. *warntype = NOWARN;
  798. if (inodes <= 0 || test_bit(DQ_FAKE_B, &dquot->dq_flags))
  799. return QUOTA_OK;
  800. if (dquot->dq_dqb.dqb_ihardlimit &&
  801. (dquot->dq_dqb.dqb_curinodes + inodes) > dquot->dq_dqb.dqb_ihardlimit &&
  802. !ignore_hardlimit(dquot)) {
  803. *warntype = IHARDWARN;
  804. return NO_QUOTA;
  805. }
  806. if (dquot->dq_dqb.dqb_isoftlimit &&
  807. (dquot->dq_dqb.dqb_curinodes + inodes) > dquot->dq_dqb.dqb_isoftlimit &&
  808. dquot->dq_dqb.dqb_itime && get_seconds() >= dquot->dq_dqb.dqb_itime &&
  809. !ignore_hardlimit(dquot)) {
  810. *warntype = ISOFTLONGWARN;
  811. return NO_QUOTA;
  812. }
  813. if (dquot->dq_dqb.dqb_isoftlimit &&
  814. (dquot->dq_dqb.dqb_curinodes + inodes) > dquot->dq_dqb.dqb_isoftlimit &&
  815. dquot->dq_dqb.dqb_itime == 0) {
  816. *warntype = ISOFTWARN;
  817. dquot->dq_dqb.dqb_itime = get_seconds() + sb_dqopt(dquot->dq_sb)->info[dquot->dq_type].dqi_igrace;
  818. }
  819. return QUOTA_OK;
  820. }
  821. /* needs dq_data_lock */
  822. static int check_bdq(struct dquot *dquot, qsize_t space, int prealloc, char *warntype)
  823. {
  824. *warntype = 0;
  825. if (space <= 0 || test_bit(DQ_FAKE_B, &dquot->dq_flags))
  826. return QUOTA_OK;
  827. if (dquot->dq_dqb.dqb_bhardlimit &&
  828. toqb(dquot->dq_dqb.dqb_curspace + space) > dquot->dq_dqb.dqb_bhardlimit &&
  829. !ignore_hardlimit(dquot)) {
  830. if (!prealloc)
  831. *warntype = BHARDWARN;
  832. return NO_QUOTA;
  833. }
  834. if (dquot->dq_dqb.dqb_bsoftlimit &&
  835. toqb(dquot->dq_dqb.dqb_curspace + space) > dquot->dq_dqb.dqb_bsoftlimit &&
  836. dquot->dq_dqb.dqb_btime && get_seconds() >= dquot->dq_dqb.dqb_btime &&
  837. !ignore_hardlimit(dquot)) {
  838. if (!prealloc)
  839. *warntype = BSOFTLONGWARN;
  840. return NO_QUOTA;
  841. }
  842. if (dquot->dq_dqb.dqb_bsoftlimit &&
  843. toqb(dquot->dq_dqb.dqb_curspace + space) > dquot->dq_dqb.dqb_bsoftlimit &&
  844. dquot->dq_dqb.dqb_btime == 0) {
  845. if (!prealloc) {
  846. *warntype = BSOFTWARN;
  847. dquot->dq_dqb.dqb_btime = get_seconds() + sb_dqopt(dquot->dq_sb)->info[dquot->dq_type].dqi_bgrace;
  848. }
  849. else
  850. /*
  851. * We don't allow preallocation to exceed softlimit so exceeding will
  852. * be always printed
  853. */
  854. return NO_QUOTA;
  855. }
  856. return QUOTA_OK;
  857. }
  858. /*
  859. * Initialize quota pointers in inode
  860. * Transaction must be started at entry
  861. */
  862. int dquot_initialize(struct inode *inode, int type)
  863. {
  864. unsigned int id = 0;
  865. int cnt, ret = 0;
  866. /* First test before acquiring mutex - solves deadlocks when we
  867. * re-enter the quota code and are already holding the mutex */
  868. if (IS_NOQUOTA(inode))
  869. return 0;
  870. down_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
  871. /* Having dqptr_sem we know NOQUOTA flags can't be altered... */
  872. if (IS_NOQUOTA(inode))
  873. goto out_err;
  874. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  875. if (type != -1 && cnt != type)
  876. continue;
  877. if (inode->i_dquot[cnt] == NODQUOT) {
  878. switch (cnt) {
  879. case USRQUOTA:
  880. id = inode->i_uid;
  881. break;
  882. case GRPQUOTA:
  883. id = inode->i_gid;
  884. break;
  885. }
  886. inode->i_dquot[cnt] = dqget(inode->i_sb, id, cnt);
  887. }
  888. }
  889. out_err:
  890. up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
  891. return ret;
  892. }
  893. /*
  894. * Release all quotas referenced by inode
  895. * Transaction must be started at an entry
  896. */
  897. int dquot_drop(struct inode *inode)
  898. {
  899. int cnt;
  900. down_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
  901. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  902. if (inode->i_dquot[cnt] != NODQUOT) {
  903. dqput(inode->i_dquot[cnt]);
  904. inode->i_dquot[cnt] = NODQUOT;
  905. }
  906. }
  907. up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
  908. return 0;
  909. }
  910. /*
  911. * Following four functions update i_blocks+i_bytes fields and
  912. * quota information (together with appropriate checks)
  913. * NOTE: We absolutely rely on the fact that caller dirties
  914. * the inode (usually macros in quotaops.h care about this) and
  915. * holds a handle for the current transaction so that dquot write and
  916. * inode write go into the same transaction.
  917. */
  918. /*
  919. * This operation can block, but only after everything is updated
  920. */
  921. int dquot_alloc_space(struct inode *inode, qsize_t number, int warn)
  922. {
  923. int cnt, ret = NO_QUOTA;
  924. char warntype[MAXQUOTAS];
  925. /* First test before acquiring mutex - solves deadlocks when we
  926. * re-enter the quota code and are already holding the mutex */
  927. if (IS_NOQUOTA(inode)) {
  928. out_add:
  929. inode_add_bytes(inode, number);
  930. return QUOTA_OK;
  931. }
  932. for (cnt = 0; cnt < MAXQUOTAS; cnt++)
  933. warntype[cnt] = NOWARN;
  934. down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  935. if (IS_NOQUOTA(inode)) { /* Now we can do reliable test... */
  936. up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  937. goto out_add;
  938. }
  939. spin_lock(&dq_data_lock);
  940. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  941. if (inode->i_dquot[cnt] == NODQUOT)
  942. continue;
  943. if (check_bdq(inode->i_dquot[cnt], number, warn, warntype+cnt) == NO_QUOTA)
  944. goto warn_put_all;
  945. }
  946. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  947. if (inode->i_dquot[cnt] == NODQUOT)
  948. continue;
  949. dquot_incr_space(inode->i_dquot[cnt], number);
  950. }
  951. inode_add_bytes(inode, number);
  952. ret = QUOTA_OK;
  953. warn_put_all:
  954. spin_unlock(&dq_data_lock);
  955. if (ret == QUOTA_OK)
  956. /* Dirtify all the dquots - this can block when journalling */
  957. for (cnt = 0; cnt < MAXQUOTAS; cnt++)
  958. if (inode->i_dquot[cnt])
  959. mark_dquot_dirty(inode->i_dquot[cnt]);
  960. flush_warnings(inode->i_dquot, warntype);
  961. up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  962. return ret;
  963. }
  964. /*
  965. * This operation can block, but only after everything is updated
  966. */
  967. int dquot_alloc_inode(const struct inode *inode, unsigned long number)
  968. {
  969. int cnt, ret = NO_QUOTA;
  970. char warntype[MAXQUOTAS];
  971. /* First test before acquiring mutex - solves deadlocks when we
  972. * re-enter the quota code and are already holding the mutex */
  973. if (IS_NOQUOTA(inode))
  974. return QUOTA_OK;
  975. for (cnt = 0; cnt < MAXQUOTAS; cnt++)
  976. warntype[cnt] = NOWARN;
  977. down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  978. if (IS_NOQUOTA(inode)) {
  979. up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  980. return QUOTA_OK;
  981. }
  982. spin_lock(&dq_data_lock);
  983. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  984. if (inode->i_dquot[cnt] == NODQUOT)
  985. continue;
  986. if (check_idq(inode->i_dquot[cnt], number, warntype+cnt) == NO_QUOTA)
  987. goto warn_put_all;
  988. }
  989. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  990. if (inode->i_dquot[cnt] == NODQUOT)
  991. continue;
  992. dquot_incr_inodes(inode->i_dquot[cnt], number);
  993. }
  994. ret = QUOTA_OK;
  995. warn_put_all:
  996. spin_unlock(&dq_data_lock);
  997. if (ret == QUOTA_OK)
  998. /* Dirtify all the dquots - this can block when journalling */
  999. for (cnt = 0; cnt < MAXQUOTAS; cnt++)
  1000. if (inode->i_dquot[cnt])
  1001. mark_dquot_dirty(inode->i_dquot[cnt]);
  1002. flush_warnings((struct dquot **)inode->i_dquot, warntype);
  1003. up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1004. return ret;
  1005. }
  1006. /*
  1007. * This operation can block, but only after everything is updated
  1008. */
  1009. int dquot_free_space(struct inode *inode, qsize_t number)
  1010. {
  1011. unsigned int cnt;
  1012. /* First test before acquiring mutex - solves deadlocks when we
  1013. * re-enter the quota code and are already holding the mutex */
  1014. if (IS_NOQUOTA(inode)) {
  1015. out_sub:
  1016. inode_sub_bytes(inode, number);
  1017. return QUOTA_OK;
  1018. }
  1019. down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1020. /* Now recheck reliably when holding dqptr_sem */
  1021. if (IS_NOQUOTA(inode)) {
  1022. up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1023. goto out_sub;
  1024. }
  1025. spin_lock(&dq_data_lock);
  1026. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1027. if (inode->i_dquot[cnt] == NODQUOT)
  1028. continue;
  1029. dquot_decr_space(inode->i_dquot[cnt], number);
  1030. }
  1031. inode_sub_bytes(inode, number);
  1032. spin_unlock(&dq_data_lock);
  1033. /* Dirtify all the dquots - this can block when journalling */
  1034. for (cnt = 0; cnt < MAXQUOTAS; cnt++)
  1035. if (inode->i_dquot[cnt])
  1036. mark_dquot_dirty(inode->i_dquot[cnt]);
  1037. up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1038. return QUOTA_OK;
  1039. }
  1040. /*
  1041. * This operation can block, but only after everything is updated
  1042. */
  1043. int dquot_free_inode(const struct inode *inode, unsigned long number)
  1044. {
  1045. unsigned int cnt;
  1046. /* First test before acquiring mutex - solves deadlocks when we
  1047. * re-enter the quota code and are already holding the mutex */
  1048. if (IS_NOQUOTA(inode))
  1049. return QUOTA_OK;
  1050. down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1051. /* Now recheck reliably when holding dqptr_sem */
  1052. if (IS_NOQUOTA(inode)) {
  1053. up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1054. return QUOTA_OK;
  1055. }
  1056. spin_lock(&dq_data_lock);
  1057. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1058. if (inode->i_dquot[cnt] == NODQUOT)
  1059. continue;
  1060. dquot_decr_inodes(inode->i_dquot[cnt], number);
  1061. }
  1062. spin_unlock(&dq_data_lock);
  1063. /* Dirtify all the dquots - this can block when journalling */
  1064. for (cnt = 0; cnt < MAXQUOTAS; cnt++)
  1065. if (inode->i_dquot[cnt])
  1066. mark_dquot_dirty(inode->i_dquot[cnt]);
  1067. up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1068. return QUOTA_OK;
  1069. }
  1070. /*
  1071. * Transfer the number of inode and blocks from one diskquota to an other.
  1072. *
  1073. * This operation can block, but only after everything is updated
  1074. * A transaction must be started when entering this function.
  1075. */
  1076. int dquot_transfer(struct inode *inode, struct iattr *iattr)
  1077. {
  1078. qsize_t space;
  1079. struct dquot *transfer_from[MAXQUOTAS];
  1080. struct dquot *transfer_to[MAXQUOTAS];
  1081. int cnt, ret = NO_QUOTA, chuid = (iattr->ia_valid & ATTR_UID) && inode->i_uid != iattr->ia_uid,
  1082. chgid = (iattr->ia_valid & ATTR_GID) && inode->i_gid != iattr->ia_gid;
  1083. char warntype[MAXQUOTAS];
  1084. /* First test before acquiring mutex - solves deadlocks when we
  1085. * re-enter the quota code and are already holding the mutex */
  1086. if (IS_NOQUOTA(inode))
  1087. return QUOTA_OK;
  1088. /* Clear the arrays */
  1089. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1090. transfer_to[cnt] = transfer_from[cnt] = NODQUOT;
  1091. warntype[cnt] = NOWARN;
  1092. }
  1093. down_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1094. /* Now recheck reliably when holding dqptr_sem */
  1095. if (IS_NOQUOTA(inode)) { /* File without quota accounting? */
  1096. up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1097. return QUOTA_OK;
  1098. }
  1099. /* First build the transfer_to list - here we can block on
  1100. * reading/instantiating of dquots. We know that the transaction for
  1101. * us was already started so we don't violate lock ranking here */
  1102. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1103. switch (cnt) {
  1104. case USRQUOTA:
  1105. if (!chuid)
  1106. continue;
  1107. transfer_to[cnt] = dqget(inode->i_sb, iattr->ia_uid, cnt);
  1108. break;
  1109. case GRPQUOTA:
  1110. if (!chgid)
  1111. continue;
  1112. transfer_to[cnt] = dqget(inode->i_sb, iattr->ia_gid, cnt);
  1113. break;
  1114. }
  1115. }
  1116. spin_lock(&dq_data_lock);
  1117. space = inode_get_bytes(inode);
  1118. /* Build the transfer_from list and check the limits */
  1119. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1120. if (transfer_to[cnt] == NODQUOT)
  1121. continue;
  1122. transfer_from[cnt] = inode->i_dquot[cnt];
  1123. if (check_idq(transfer_to[cnt], 1, warntype+cnt) == NO_QUOTA ||
  1124. check_bdq(transfer_to[cnt], space, 0, warntype+cnt) == NO_QUOTA)
  1125. goto warn_put_all;
  1126. }
  1127. /*
  1128. * Finally perform the needed transfer from transfer_from to transfer_to
  1129. */
  1130. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1131. /*
  1132. * Skip changes for same uid or gid or for turned off quota-type.
  1133. */
  1134. if (transfer_to[cnt] == NODQUOT)
  1135. continue;
  1136. /* Due to IO error we might not have transfer_from[] structure */
  1137. if (transfer_from[cnt]) {
  1138. dquot_decr_inodes(transfer_from[cnt], 1);
  1139. dquot_decr_space(transfer_from[cnt], space);
  1140. }
  1141. dquot_incr_inodes(transfer_to[cnt], 1);
  1142. dquot_incr_space(transfer_to[cnt], space);
  1143. inode->i_dquot[cnt] = transfer_to[cnt];
  1144. }
  1145. ret = QUOTA_OK;
  1146. warn_put_all:
  1147. spin_unlock(&dq_data_lock);
  1148. /* Dirtify all the dquots - this can block when journalling */
  1149. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1150. if (transfer_from[cnt])
  1151. mark_dquot_dirty(transfer_from[cnt]);
  1152. if (transfer_to[cnt])
  1153. mark_dquot_dirty(transfer_to[cnt]);
  1154. }
  1155. flush_warnings(transfer_to, warntype);
  1156. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1157. if (ret == QUOTA_OK && transfer_from[cnt] != NODQUOT)
  1158. dqput(transfer_from[cnt]);
  1159. if (ret == NO_QUOTA && transfer_to[cnt] != NODQUOT)
  1160. dqput(transfer_to[cnt]);
  1161. }
  1162. up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1163. return ret;
  1164. }
  1165. /*
  1166. * Write info of quota file to disk
  1167. */
  1168. int dquot_commit_info(struct super_block *sb, int type)
  1169. {
  1170. int ret;
  1171. struct quota_info *dqopt = sb_dqopt(sb);
  1172. mutex_lock(&dqopt->dqio_mutex);
  1173. ret = dqopt->ops[type]->write_file_info(sb, type);
  1174. mutex_unlock(&dqopt->dqio_mutex);
  1175. return ret;
  1176. }
  1177. /*
  1178. * Definitions of diskquota operations.
  1179. */
  1180. struct dquot_operations dquot_operations = {
  1181. .initialize = dquot_initialize,
  1182. .drop = dquot_drop,
  1183. .alloc_space = dquot_alloc_space,
  1184. .alloc_inode = dquot_alloc_inode,
  1185. .free_space = dquot_free_space,
  1186. .free_inode = dquot_free_inode,
  1187. .transfer = dquot_transfer,
  1188. .write_dquot = dquot_commit,
  1189. .acquire_dquot = dquot_acquire,
  1190. .release_dquot = dquot_release,
  1191. .mark_dirty = dquot_mark_dquot_dirty,
  1192. .write_info = dquot_commit_info
  1193. };
  1194. static inline void set_enable_flags(struct quota_info *dqopt, int type)
  1195. {
  1196. switch (type) {
  1197. case USRQUOTA:
  1198. dqopt->flags |= DQUOT_USR_ENABLED;
  1199. break;
  1200. case GRPQUOTA:
  1201. dqopt->flags |= DQUOT_GRP_ENABLED;
  1202. break;
  1203. }
  1204. }
  1205. static inline void reset_enable_flags(struct quota_info *dqopt, int type)
  1206. {
  1207. switch (type) {
  1208. case USRQUOTA:
  1209. dqopt->flags &= ~DQUOT_USR_ENABLED;
  1210. break;
  1211. case GRPQUOTA:
  1212. dqopt->flags &= ~DQUOT_GRP_ENABLED;
  1213. break;
  1214. }
  1215. }
  1216. /*
  1217. * Turn quota off on a device. type == -1 ==> quotaoff for all types (umount)
  1218. */
  1219. int vfs_quota_off(struct super_block *sb, int type)
  1220. {
  1221. int cnt;
  1222. struct quota_info *dqopt = sb_dqopt(sb);
  1223. struct inode *toputinode[MAXQUOTAS];
  1224. /* We need to serialize quota_off() for device */
  1225. mutex_lock(&dqopt->dqonoff_mutex);
  1226. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1227. toputinode[cnt] = NULL;
  1228. if (type != -1 && cnt != type)
  1229. continue;
  1230. if (!sb_has_quota_enabled(sb, cnt))
  1231. continue;
  1232. reset_enable_flags(dqopt, cnt);
  1233. /* Note: these are blocking operations */
  1234. drop_dquot_ref(sb, cnt);
  1235. invalidate_dquots(sb, cnt);
  1236. /*
  1237. * Now all dquots should be invalidated, all writes done so we should be only
  1238. * users of the info. No locks needed.
  1239. */
  1240. if (info_dirty(&dqopt->info[cnt]))
  1241. sb->dq_op->write_info(sb, cnt);
  1242. if (dqopt->ops[cnt]->free_file_info)
  1243. dqopt->ops[cnt]->free_file_info(sb, cnt);
  1244. put_quota_format(dqopt->info[cnt].dqi_format);
  1245. toputinode[cnt] = dqopt->files[cnt];
  1246. dqopt->files[cnt] = NULL;
  1247. dqopt->info[cnt].dqi_flags = 0;
  1248. dqopt->info[cnt].dqi_igrace = 0;
  1249. dqopt->info[cnt].dqi_bgrace = 0;
  1250. dqopt->ops[cnt] = NULL;
  1251. }
  1252. mutex_unlock(&dqopt->dqonoff_mutex);
  1253. /* Sync the superblock so that buffers with quota data are written to
  1254. * disk (and so userspace sees correct data afterwards). */
  1255. if (sb->s_op->sync_fs)
  1256. sb->s_op->sync_fs(sb, 1);
  1257. sync_blockdev(sb->s_bdev);
  1258. /* Now the quota files are just ordinary files and we can set the
  1259. * inode flags back. Moreover we discard the pagecache so that
  1260. * userspace sees the writes we did bypassing the pagecache. We
  1261. * must also discard the blockdev buffers so that we see the
  1262. * changes done by userspace on the next quotaon() */
  1263. for (cnt = 0; cnt < MAXQUOTAS; cnt++)
  1264. if (toputinode[cnt]) {
  1265. mutex_lock(&dqopt->dqonoff_mutex);
  1266. /* If quota was reenabled in the meantime, we have
  1267. * nothing to do */
  1268. if (!sb_has_quota_enabled(sb, cnt)) {
  1269. mutex_lock(&toputinode[cnt]->i_mutex);
  1270. toputinode[cnt]->i_flags &= ~(S_IMMUTABLE |
  1271. S_NOATIME | S_NOQUOTA);
  1272. truncate_inode_pages(&toputinode[cnt]->i_data, 0);
  1273. mutex_unlock(&toputinode[cnt]->i_mutex);
  1274. mark_inode_dirty(toputinode[cnt]);
  1275. iput(toputinode[cnt]);
  1276. }
  1277. mutex_unlock(&dqopt->dqonoff_mutex);
  1278. }
  1279. if (sb->s_bdev)
  1280. invalidate_bdev(sb->s_bdev, 0);
  1281. return 0;
  1282. }
  1283. /*
  1284. * Turn quotas on on a device
  1285. */
  1286. /* Helper function when we already have the inode */
  1287. static int vfs_quota_on_inode(struct inode *inode, int type, int format_id)
  1288. {
  1289. struct quota_format_type *fmt = find_quota_format(format_id);
  1290. struct super_block *sb = inode->i_sb;
  1291. struct quota_info *dqopt = sb_dqopt(sb);
  1292. int error;
  1293. int oldflags = -1;
  1294. if (!fmt)
  1295. return -ESRCH;
  1296. if (!S_ISREG(inode->i_mode)) {
  1297. error = -EACCES;
  1298. goto out_fmt;
  1299. }
  1300. if (IS_RDONLY(inode)) {
  1301. error = -EROFS;
  1302. goto out_fmt;
  1303. }
  1304. if (!sb->s_op->quota_write || !sb->s_op->quota_read) {
  1305. error = -EINVAL;
  1306. goto out_fmt;
  1307. }
  1308. /* As we bypass the pagecache we must now flush the inode so that
  1309. * we see all the changes from userspace... */
  1310. write_inode_now(inode, 1);
  1311. /* And now flush the block cache so that kernel sees the changes */
  1312. invalidate_bdev(sb->s_bdev, 0);
  1313. mutex_lock(&inode->i_mutex);
  1314. mutex_lock(&dqopt->dqonoff_mutex);
  1315. if (sb_has_quota_enabled(sb, type)) {
  1316. error = -EBUSY;
  1317. goto out_lock;
  1318. }
  1319. /* We don't want quota and atime on quota files (deadlocks possible)
  1320. * Also nobody should write to the file - we use special IO operations
  1321. * which ignore the immutable bit. */
  1322. down_write(&dqopt->dqptr_sem);
  1323. oldflags = inode->i_flags & (S_NOATIME | S_IMMUTABLE | S_NOQUOTA);
  1324. inode->i_flags |= S_NOQUOTA | S_NOATIME | S_IMMUTABLE;
  1325. up_write(&dqopt->dqptr_sem);
  1326. sb->dq_op->drop(inode);
  1327. error = -EIO;
  1328. dqopt->files[type] = igrab(inode);
  1329. if (!dqopt->files[type])
  1330. goto out_lock;
  1331. error = -EINVAL;
  1332. if (!fmt->qf_ops->check_quota_file(sb, type))
  1333. goto out_file_init;
  1334. dqopt->ops[type] = fmt->qf_ops;
  1335. dqopt->info[type].dqi_format = fmt;
  1336. INIT_LIST_HEAD(&dqopt->info[type].dqi_dirty_list);
  1337. mutex_lock(&dqopt->dqio_mutex);
  1338. if ((error = dqopt->ops[type]->read_file_info(sb, type)) < 0) {
  1339. mutex_unlock(&dqopt->dqio_mutex);
  1340. goto out_file_init;
  1341. }
  1342. mutex_unlock(&dqopt->dqio_mutex);
  1343. mutex_unlock(&inode->i_mutex);
  1344. set_enable_flags(dqopt, type);
  1345. add_dquot_ref(sb, type);
  1346. mutex_unlock(&dqopt->dqonoff_mutex);
  1347. return 0;
  1348. out_file_init:
  1349. dqopt->files[type] = NULL;
  1350. iput(inode);
  1351. out_lock:
  1352. mutex_unlock(&dqopt->dqonoff_mutex);
  1353. if (oldflags != -1) {
  1354. down_write(&dqopt->dqptr_sem);
  1355. /* Set the flags back (in the case of accidental quotaon()
  1356. * on a wrong file we don't want to mess up the flags) */
  1357. inode->i_flags &= ~(S_NOATIME | S_NOQUOTA | S_IMMUTABLE);
  1358. inode->i_flags |= oldflags;
  1359. up_write(&dqopt->dqptr_sem);
  1360. }
  1361. mutex_unlock(&inode->i_mutex);
  1362. out_fmt:
  1363. put_quota_format(fmt);
  1364. return error;
  1365. }
  1366. /* Actual function called from quotactl() */
  1367. int vfs_quota_on(struct super_block *sb, int type, int format_id, char *path)
  1368. {
  1369. struct nameidata nd;
  1370. int error;
  1371. error = path_lookup(path, LOOKUP_FOLLOW, &nd);
  1372. if (error < 0)
  1373. return error;
  1374. error = security_quota_on(nd.dentry);
  1375. if (error)
  1376. goto out_path;
  1377. /* Quota file not on the same filesystem? */
  1378. if (nd.mnt->mnt_sb != sb)
  1379. error = -EXDEV;
  1380. else
  1381. error = vfs_quota_on_inode(nd.dentry->d_inode, type, format_id);
  1382. out_path:
  1383. path_release(&nd);
  1384. return error;
  1385. }
  1386. /*
  1387. * This function is used when filesystem needs to initialize quotas
  1388. * during mount time.
  1389. */
  1390. int vfs_quota_on_mount(struct super_block *sb, char *qf_name,
  1391. int format_id, int type)
  1392. {
  1393. struct dentry *dentry;
  1394. int error;
  1395. dentry = lookup_one_len(qf_name, sb->s_root, strlen(qf_name));
  1396. if (IS_ERR(dentry))
  1397. return PTR_ERR(dentry);
  1398. if (!dentry->d_inode) {
  1399. error = -ENOENT;
  1400. goto out;
  1401. }
  1402. error = security_quota_on(dentry);
  1403. if (!error)
  1404. error = vfs_quota_on_inode(dentry->d_inode, type, format_id);
  1405. out:
  1406. dput(dentry);
  1407. return error;
  1408. }
  1409. /* Generic routine for getting common part of quota structure */
  1410. static void do_get_dqblk(struct dquot *dquot, struct if_dqblk *di)
  1411. {
  1412. struct mem_dqblk *dm = &dquot->dq_dqb;
  1413. spin_lock(&dq_data_lock);
  1414. di->dqb_bhardlimit = dm->dqb_bhardlimit;
  1415. di->dqb_bsoftlimit = dm->dqb_bsoftlimit;
  1416. di->dqb_curspace = dm->dqb_curspace;
  1417. di->dqb_ihardlimit = dm->dqb_ihardlimit;
  1418. di->dqb_isoftlimit = dm->dqb_isoftlimit;
  1419. di->dqb_curinodes = dm->dqb_curinodes;
  1420. di->dqb_btime = dm->dqb_btime;
  1421. di->dqb_itime = dm->dqb_itime;
  1422. di->dqb_valid = QIF_ALL;
  1423. spin_unlock(&dq_data_lock);
  1424. }
  1425. int vfs_get_dqblk(struct super_block *sb, int type, qid_t id, struct if_dqblk *di)
  1426. {
  1427. struct dquot *dquot;
  1428. mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
  1429. if (!(dquot = dqget(sb, id, type))) {
  1430. mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
  1431. return -ESRCH;
  1432. }
  1433. do_get_dqblk(dquot, di);
  1434. dqput(dquot);
  1435. mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
  1436. return 0;
  1437. }
  1438. /* Generic routine for setting common part of quota structure */
  1439. static void do_set_dqblk(struct dquot *dquot, struct if_dqblk *di)
  1440. {
  1441. struct mem_dqblk *dm = &dquot->dq_dqb;
  1442. int check_blim = 0, check_ilim = 0;
  1443. spin_lock(&dq_data_lock);
  1444. if (di->dqb_valid & QIF_SPACE) {
  1445. dm->dqb_curspace = di->dqb_curspace;
  1446. check_blim = 1;
  1447. }
  1448. if (di->dqb_valid & QIF_BLIMITS) {
  1449. dm->dqb_bsoftlimit = di->dqb_bsoftlimit;
  1450. dm->dqb_bhardlimit = di->dqb_bhardlimit;
  1451. check_blim = 1;
  1452. }
  1453. if (di->dqb_valid & QIF_INODES) {
  1454. dm->dqb_curinodes = di->dqb_curinodes;
  1455. check_ilim = 1;
  1456. }
  1457. if (di->dqb_valid & QIF_ILIMITS) {
  1458. dm->dqb_isoftlimit = di->dqb_isoftlimit;
  1459. dm->dqb_ihardlimit = di->dqb_ihardlimit;
  1460. check_ilim = 1;
  1461. }
  1462. if (di->dqb_valid & QIF_BTIME)
  1463. dm->dqb_btime = di->dqb_btime;
  1464. if (di->dqb_valid & QIF_ITIME)
  1465. dm->dqb_itime = di->dqb_itime;
  1466. if (check_blim) {
  1467. if (!dm->dqb_bsoftlimit || toqb(dm->dqb_curspace) < dm->dqb_bsoftlimit) {
  1468. dm->dqb_btime = 0;
  1469. clear_bit(DQ_BLKS_B, &dquot->dq_flags);
  1470. }
  1471. else if (!(di->dqb_valid & QIF_BTIME)) /* Set grace only if user hasn't provided his own... */
  1472. dm->dqb_btime = get_seconds() + sb_dqopt(dquot->dq_sb)->info[dquot->dq_type].dqi_bgrace;
  1473. }
  1474. if (check_ilim) {
  1475. if (!dm->dqb_isoftlimit || dm->dqb_curinodes < dm->dqb_isoftlimit) {
  1476. dm->dqb_itime = 0;
  1477. clear_bit(DQ_INODES_B, &dquot->dq_flags);
  1478. }
  1479. else if (!(di->dqb_valid & QIF_ITIME)) /* Set grace only if user hasn't provided his own... */
  1480. dm->dqb_itime = get_seconds() + sb_dqopt(dquot->dq_sb)->info[dquot->dq_type].dqi_igrace;
  1481. }
  1482. if (dm->dqb_bhardlimit || dm->dqb_bsoftlimit || dm->dqb_ihardlimit || dm->dqb_isoftlimit)
  1483. clear_bit(DQ_FAKE_B, &dquot->dq_flags);
  1484. else
  1485. set_bit(DQ_FAKE_B, &dquot->dq_flags);
  1486. spin_unlock(&dq_data_lock);
  1487. mark_dquot_dirty(dquot);
  1488. }
  1489. int vfs_set_dqblk(struct super_block *sb, int type, qid_t id, struct if_dqblk *di)
  1490. {
  1491. struct dquot *dquot;
  1492. mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
  1493. if (!(dquot = dqget(sb, id, type))) {
  1494. mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
  1495. return -ESRCH;
  1496. }
  1497. do_set_dqblk(dquot, di);
  1498. dqput(dquot);
  1499. mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
  1500. return 0;
  1501. }
  1502. /* Generic routine for getting common part of quota file information */
  1503. int vfs_get_dqinfo(struct super_block *sb, int type, struct if_dqinfo *ii)
  1504. {
  1505. struct mem_dqinfo *mi;
  1506. mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
  1507. if (!sb_has_quota_enabled(sb, type)) {
  1508. mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
  1509. return -ESRCH;
  1510. }
  1511. mi = sb_dqopt(sb)->info + type;
  1512. spin_lock(&dq_data_lock);
  1513. ii->dqi_bgrace = mi->dqi_bgrace;
  1514. ii->dqi_igrace = mi->dqi_igrace;
  1515. ii->dqi_flags = mi->dqi_flags & DQF_MASK;
  1516. ii->dqi_valid = IIF_ALL;
  1517. spin_unlock(&dq_data_lock);
  1518. mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
  1519. return 0;
  1520. }
  1521. /* Generic routine for setting common part of quota file information */
  1522. int vfs_set_dqinfo(struct super_block *sb, int type, struct if_dqinfo *ii)
  1523. {
  1524. struct mem_dqinfo *mi;
  1525. mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
  1526. if (!sb_has_quota_enabled(sb, type)) {
  1527. mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
  1528. return -ESRCH;
  1529. }
  1530. mi = sb_dqopt(sb)->info + type;
  1531. spin_lock(&dq_data_lock);
  1532. if (ii->dqi_valid & IIF_BGRACE)
  1533. mi->dqi_bgrace = ii->dqi_bgrace;
  1534. if (ii->dqi_valid & IIF_IGRACE)
  1535. mi->dqi_igrace = ii->dqi_igrace;
  1536. if (ii->dqi_valid & IIF_FLAGS)
  1537. mi->dqi_flags = (mi->dqi_flags & ~DQF_MASK) | (ii->dqi_flags & DQF_MASK);
  1538. spin_unlock(&dq_data_lock);
  1539. mark_info_dirty(sb, type);
  1540. /* Force write to disk */
  1541. sb->dq_op->write_info(sb, type);
  1542. mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
  1543. return 0;
  1544. }
  1545. struct quotactl_ops vfs_quotactl_ops = {
  1546. .quota_on = vfs_quota_on,
  1547. .quota_off = vfs_quota_off,
  1548. .quota_sync = vfs_quota_sync,
  1549. .get_info = vfs_get_dqinfo,
  1550. .set_info = vfs_set_dqinfo,
  1551. .get_dqblk = vfs_get_dqblk,
  1552. .set_dqblk = vfs_set_dqblk
  1553. };
  1554. static ctl_table fs_dqstats_table[] = {
  1555. {
  1556. .ctl_name = FS_DQ_LOOKUPS,
  1557. .procname = "lookups",
  1558. .data = &dqstats.lookups,
  1559. .maxlen = sizeof(int),
  1560. .mode = 0444,
  1561. .proc_handler = &proc_dointvec,
  1562. },
  1563. {
  1564. .ctl_name = FS_DQ_DROPS,
  1565. .procname = "drops",
  1566. .data = &dqstats.drops,
  1567. .maxlen = sizeof(int),
  1568. .mode = 0444,
  1569. .proc_handler = &proc_dointvec,
  1570. },
  1571. {
  1572. .ctl_name = FS_DQ_READS,
  1573. .procname = "reads",
  1574. .data = &dqstats.reads,
  1575. .maxlen = sizeof(int),
  1576. .mode = 0444,
  1577. .proc_handler = &proc_dointvec,
  1578. },
  1579. {
  1580. .ctl_name = FS_DQ_WRITES,
  1581. .procname = "writes",
  1582. .data = &dqstats.writes,
  1583. .maxlen = sizeof(int),
  1584. .mode = 0444,
  1585. .proc_handler = &proc_dointvec,
  1586. },
  1587. {
  1588. .ctl_name = FS_DQ_CACHE_HITS,
  1589. .procname = "cache_hits",
  1590. .data = &dqstats.cache_hits,
  1591. .maxlen = sizeof(int),
  1592. .mode = 0444,
  1593. .proc_handler = &proc_dointvec,
  1594. },
  1595. {
  1596. .ctl_name = FS_DQ_ALLOCATED,
  1597. .procname = "allocated_dquots",
  1598. .data = &dqstats.allocated_dquots,
  1599. .maxlen = sizeof(int),
  1600. .mode = 0444,
  1601. .proc_handler = &proc_dointvec,
  1602. },
  1603. {
  1604. .ctl_name = FS_DQ_FREE,
  1605. .procname = "free_dquots",
  1606. .data = &dqstats.free_dquots,
  1607. .maxlen = sizeof(int),
  1608. .mode = 0444,
  1609. .proc_handler = &proc_dointvec,
  1610. },
  1611. {
  1612. .ctl_name = FS_DQ_SYNCS,
  1613. .procname = "syncs",
  1614. .data = &dqstats.syncs,
  1615. .maxlen = sizeof(int),
  1616. .mode = 0444,
  1617. .proc_handler = &proc_dointvec,
  1618. },
  1619. {
  1620. .ctl_name = FS_DQ_WARNINGS,
  1621. .procname = "warnings",
  1622. .data = &flag_print_warnings,
  1623. .maxlen = sizeof(int),
  1624. .mode = 0644,
  1625. .proc_handler = &proc_dointvec,
  1626. },
  1627. { .ctl_name = 0 },
  1628. };
  1629. static ctl_table fs_table[] = {
  1630. {
  1631. .ctl_name = FS_DQSTATS,
  1632. .procname = "quota",
  1633. .mode = 0555,
  1634. .child = fs_dqstats_table,
  1635. },
  1636. { .ctl_name = 0 },
  1637. };
  1638. static ctl_table sys_table[] = {
  1639. {
  1640. .ctl_name = CTL_FS,
  1641. .procname = "fs",
  1642. .mode = 0555,
  1643. .child = fs_table,
  1644. },
  1645. { .ctl_name = 0 },
  1646. };
  1647. static int __init dquot_init(void)
  1648. {
  1649. int i;
  1650. unsigned long nr_hash, order;
  1651. printk(KERN_NOTICE "VFS: Disk quotas %s\n", __DQUOT_VERSION__);
  1652. register_sysctl_table(sys_table, 0);
  1653. dquot_cachep = kmem_cache_create("dquot",
  1654. sizeof(struct dquot), sizeof(unsigned long) * 4,
  1655. (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
  1656. SLAB_MEM_SPREAD|SLAB_PANIC),
  1657. NULL, NULL);
  1658. order = 0;
  1659. dquot_hash = (struct hlist_head *)__get_free_pages(GFP_ATOMIC, order);
  1660. if (!dquot_hash)
  1661. panic("Cannot create dquot hash table");
  1662. /* Find power-of-two hlist_heads which can fit into allocation */
  1663. nr_hash = (1UL << order) * PAGE_SIZE / sizeof(struct hlist_head);
  1664. dq_hash_bits = 0;
  1665. do {
  1666. dq_hash_bits++;
  1667. } while (nr_hash >> dq_hash_bits);
  1668. dq_hash_bits--;
  1669. nr_hash = 1UL << dq_hash_bits;
  1670. dq_hash_mask = nr_hash - 1;
  1671. for (i = 0; i < nr_hash; i++)
  1672. INIT_HLIST_HEAD(dquot_hash + i);
  1673. printk("Dquot-cache hash table entries: %ld (order %ld, %ld bytes)\n",
  1674. nr_hash, order, (PAGE_SIZE << order));
  1675. set_shrinker(DEFAULT_SEEKS, shrink_dqcache_memory);
  1676. return 0;
  1677. }
  1678. module_init(dquot_init);
  1679. EXPORT_SYMBOL(register_quota_format);
  1680. EXPORT_SYMBOL(unregister_quota_format);
  1681. EXPORT_SYMBOL(dqstats);
  1682. EXPORT_SYMBOL(dq_data_lock);
  1683. EXPORT_SYMBOL(vfs_quota_on);
  1684. EXPORT_SYMBOL(vfs_quota_on_mount);
  1685. EXPORT_SYMBOL(vfs_quota_off);
  1686. EXPORT_SYMBOL(vfs_quota_sync);
  1687. EXPORT_SYMBOL(vfs_get_dqinfo);
  1688. EXPORT_SYMBOL(vfs_set_dqinfo);
  1689. EXPORT_SYMBOL(vfs_get_dqblk);
  1690. EXPORT_SYMBOL(vfs_set_dqblk);
  1691. EXPORT_SYMBOL(dquot_commit);
  1692. EXPORT_SYMBOL(dquot_commit_info);
  1693. EXPORT_SYMBOL(dquot_acquire);
  1694. EXPORT_SYMBOL(dquot_release);
  1695. EXPORT_SYMBOL(dquot_mark_dquot_dirty);
  1696. EXPORT_SYMBOL(dquot_initialize);
  1697. EXPORT_SYMBOL(dquot_drop);
  1698. EXPORT_SYMBOL(dquot_alloc_space);
  1699. EXPORT_SYMBOL(dquot_alloc_inode);
  1700. EXPORT_SYMBOL(dquot_free_space);
  1701. EXPORT_SYMBOL(dquot_free_inode);
  1702. EXPORT_SYMBOL(dquot_transfer);