dquot.c 53 KB

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