dquot.c 62 KB

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