dquot.c 62 KB

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