dquot.c 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293
  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_active(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_active(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_active(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_active(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 (!sb_has_quota_limits_enabled(dquot->dq_sb, dquot->dq_type) ||
  940. test_bit(DQ_FAKE_B, &dquot->dq_flags))
  941. return QUOTA_OK;
  942. if (dquot->dq_dqb.dqb_ihardlimit &&
  943. (dquot->dq_dqb.dqb_curinodes + inodes) > dquot->dq_dqb.dqb_ihardlimit &&
  944. !ignore_hardlimit(dquot)) {
  945. *warntype = QUOTA_NL_IHARDWARN;
  946. return NO_QUOTA;
  947. }
  948. if (dquot->dq_dqb.dqb_isoftlimit &&
  949. (dquot->dq_dqb.dqb_curinodes + inodes) > dquot->dq_dqb.dqb_isoftlimit &&
  950. dquot->dq_dqb.dqb_itime && get_seconds() >= dquot->dq_dqb.dqb_itime &&
  951. !ignore_hardlimit(dquot)) {
  952. *warntype = QUOTA_NL_ISOFTLONGWARN;
  953. return NO_QUOTA;
  954. }
  955. if (dquot->dq_dqb.dqb_isoftlimit &&
  956. (dquot->dq_dqb.dqb_curinodes + inodes) > dquot->dq_dqb.dqb_isoftlimit &&
  957. dquot->dq_dqb.dqb_itime == 0) {
  958. *warntype = QUOTA_NL_ISOFTWARN;
  959. dquot->dq_dqb.dqb_itime = get_seconds() + sb_dqopt(dquot->dq_sb)->info[dquot->dq_type].dqi_igrace;
  960. }
  961. return QUOTA_OK;
  962. }
  963. /* needs dq_data_lock */
  964. static int check_bdq(struct dquot *dquot, qsize_t space, int prealloc, char *warntype)
  965. {
  966. *warntype = QUOTA_NL_NOWARN;
  967. if (!sb_has_quota_limits_enabled(dquot->dq_sb, dquot->dq_type) ||
  968. test_bit(DQ_FAKE_B, &dquot->dq_flags))
  969. return QUOTA_OK;
  970. if (dquot->dq_dqb.dqb_bhardlimit &&
  971. dquot->dq_dqb.dqb_curspace + space > dquot->dq_dqb.dqb_bhardlimit &&
  972. !ignore_hardlimit(dquot)) {
  973. if (!prealloc)
  974. *warntype = QUOTA_NL_BHARDWARN;
  975. return NO_QUOTA;
  976. }
  977. if (dquot->dq_dqb.dqb_bsoftlimit &&
  978. dquot->dq_dqb.dqb_curspace + space > dquot->dq_dqb.dqb_bsoftlimit &&
  979. dquot->dq_dqb.dqb_btime && get_seconds() >= dquot->dq_dqb.dqb_btime &&
  980. !ignore_hardlimit(dquot)) {
  981. if (!prealloc)
  982. *warntype = QUOTA_NL_BSOFTLONGWARN;
  983. return NO_QUOTA;
  984. }
  985. if (dquot->dq_dqb.dqb_bsoftlimit &&
  986. dquot->dq_dqb.dqb_curspace + space > dquot->dq_dqb.dqb_bsoftlimit &&
  987. dquot->dq_dqb.dqb_btime == 0) {
  988. if (!prealloc) {
  989. *warntype = QUOTA_NL_BSOFTWARN;
  990. dquot->dq_dqb.dqb_btime = get_seconds() + sb_dqopt(dquot->dq_sb)->info[dquot->dq_type].dqi_bgrace;
  991. }
  992. else
  993. /*
  994. * We don't allow preallocation to exceed softlimit so exceeding will
  995. * be always printed
  996. */
  997. return NO_QUOTA;
  998. }
  999. return QUOTA_OK;
  1000. }
  1001. static int info_idq_free(struct dquot *dquot, qsize_t inodes)
  1002. {
  1003. if (test_bit(DQ_FAKE_B, &dquot->dq_flags) ||
  1004. dquot->dq_dqb.dqb_curinodes <= dquot->dq_dqb.dqb_isoftlimit ||
  1005. !sb_has_quota_limits_enabled(dquot->dq_sb, dquot->dq_type))
  1006. return QUOTA_NL_NOWARN;
  1007. if (dquot->dq_dqb.dqb_curinodes - inodes <= dquot->dq_dqb.dqb_isoftlimit)
  1008. return QUOTA_NL_ISOFTBELOW;
  1009. if (dquot->dq_dqb.dqb_curinodes >= dquot->dq_dqb.dqb_ihardlimit &&
  1010. dquot->dq_dqb.dqb_curinodes - inodes < dquot->dq_dqb.dqb_ihardlimit)
  1011. return QUOTA_NL_IHARDBELOW;
  1012. return QUOTA_NL_NOWARN;
  1013. }
  1014. static int info_bdq_free(struct dquot *dquot, qsize_t space)
  1015. {
  1016. if (test_bit(DQ_FAKE_B, &dquot->dq_flags) ||
  1017. dquot->dq_dqb.dqb_curspace <= dquot->dq_dqb.dqb_bsoftlimit)
  1018. return QUOTA_NL_NOWARN;
  1019. if (dquot->dq_dqb.dqb_curspace - space <= dquot->dq_dqb.dqb_bsoftlimit)
  1020. return QUOTA_NL_BSOFTBELOW;
  1021. if (dquot->dq_dqb.dqb_curspace >= dquot->dq_dqb.dqb_bhardlimit &&
  1022. dquot->dq_dqb.dqb_curspace - space < dquot->dq_dqb.dqb_bhardlimit)
  1023. return QUOTA_NL_BHARDBELOW;
  1024. return QUOTA_NL_NOWARN;
  1025. }
  1026. /*
  1027. * Initialize quota pointers in inode
  1028. * Transaction must be started at entry
  1029. */
  1030. int dquot_initialize(struct inode *inode, int type)
  1031. {
  1032. unsigned int id = 0;
  1033. int cnt, ret = 0;
  1034. /* First test before acquiring mutex - solves deadlocks when we
  1035. * re-enter the quota code and are already holding the mutex */
  1036. if (IS_NOQUOTA(inode))
  1037. return 0;
  1038. down_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1039. /* Having dqptr_sem we know NOQUOTA flags can't be altered... */
  1040. if (IS_NOQUOTA(inode))
  1041. goto out_err;
  1042. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1043. if (type != -1 && cnt != type)
  1044. continue;
  1045. if (inode->i_dquot[cnt] == NODQUOT) {
  1046. switch (cnt) {
  1047. case USRQUOTA:
  1048. id = inode->i_uid;
  1049. break;
  1050. case GRPQUOTA:
  1051. id = inode->i_gid;
  1052. break;
  1053. }
  1054. inode->i_dquot[cnt] = dqget(inode->i_sb, id, cnt);
  1055. }
  1056. }
  1057. out_err:
  1058. up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1059. return ret;
  1060. }
  1061. /*
  1062. * Release all quotas referenced by inode
  1063. * Transaction must be started at an entry
  1064. */
  1065. int dquot_drop(struct inode *inode)
  1066. {
  1067. int cnt;
  1068. down_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1069. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1070. if (inode->i_dquot[cnt] != NODQUOT) {
  1071. dqput(inode->i_dquot[cnt]);
  1072. inode->i_dquot[cnt] = NODQUOT;
  1073. }
  1074. }
  1075. up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1076. return 0;
  1077. }
  1078. /* Wrapper to remove references to quota structures from inode */
  1079. void vfs_dq_drop(struct inode *inode)
  1080. {
  1081. /* Here we can get arbitrary inode from clear_inode() so we have
  1082. * to be careful. OTOH we don't need locking as quota operations
  1083. * are allowed to change only at mount time */
  1084. if (!IS_NOQUOTA(inode) && inode->i_sb && inode->i_sb->dq_op
  1085. && inode->i_sb->dq_op->drop) {
  1086. int cnt;
  1087. /* Test before calling to rule out calls from proc and such
  1088. * where we are not allowed to block. Note that this is
  1089. * actually reliable test even without the lock - the caller
  1090. * must assure that nobody can come after the DQUOT_DROP and
  1091. * add quota pointers back anyway */
  1092. for (cnt = 0; cnt < MAXQUOTAS; cnt++)
  1093. if (inode->i_dquot[cnt] != NODQUOT)
  1094. break;
  1095. if (cnt < MAXQUOTAS)
  1096. inode->i_sb->dq_op->drop(inode);
  1097. }
  1098. }
  1099. /*
  1100. * Following four functions update i_blocks+i_bytes fields and
  1101. * quota information (together with appropriate checks)
  1102. * NOTE: We absolutely rely on the fact that caller dirties
  1103. * the inode (usually macros in quotaops.h care about this) and
  1104. * holds a handle for the current transaction so that dquot write and
  1105. * inode write go into the same transaction.
  1106. */
  1107. /*
  1108. * This operation can block, but only after everything is updated
  1109. */
  1110. int dquot_alloc_space(struct inode *inode, qsize_t number, int warn)
  1111. {
  1112. int cnt, ret = NO_QUOTA;
  1113. char warntype[MAXQUOTAS];
  1114. /* First test before acquiring mutex - solves deadlocks when we
  1115. * re-enter the quota code and are already holding the mutex */
  1116. if (IS_NOQUOTA(inode)) {
  1117. out_add:
  1118. inode_add_bytes(inode, number);
  1119. return QUOTA_OK;
  1120. }
  1121. for (cnt = 0; cnt < MAXQUOTAS; cnt++)
  1122. warntype[cnt] = QUOTA_NL_NOWARN;
  1123. down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1124. if (IS_NOQUOTA(inode)) { /* Now we can do reliable test... */
  1125. up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1126. goto out_add;
  1127. }
  1128. spin_lock(&dq_data_lock);
  1129. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1130. if (inode->i_dquot[cnt] == NODQUOT)
  1131. continue;
  1132. if (check_bdq(inode->i_dquot[cnt], number, warn, warntype+cnt) == NO_QUOTA)
  1133. goto warn_put_all;
  1134. }
  1135. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1136. if (inode->i_dquot[cnt] == NODQUOT)
  1137. continue;
  1138. dquot_incr_space(inode->i_dquot[cnt], number);
  1139. }
  1140. inode_add_bytes(inode, number);
  1141. ret = QUOTA_OK;
  1142. warn_put_all:
  1143. spin_unlock(&dq_data_lock);
  1144. if (ret == QUOTA_OK)
  1145. /* Dirtify all the dquots - this can block when journalling */
  1146. for (cnt = 0; cnt < MAXQUOTAS; cnt++)
  1147. if (inode->i_dquot[cnt])
  1148. mark_dquot_dirty(inode->i_dquot[cnt]);
  1149. flush_warnings(inode->i_dquot, warntype);
  1150. up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1151. return ret;
  1152. }
  1153. /*
  1154. * This operation can block, but only after everything is updated
  1155. */
  1156. int dquot_alloc_inode(const struct inode *inode, qsize_t number)
  1157. {
  1158. int cnt, ret = NO_QUOTA;
  1159. char warntype[MAXQUOTAS];
  1160. /* First test before acquiring mutex - solves deadlocks when we
  1161. * re-enter the quota code and are already holding the mutex */
  1162. if (IS_NOQUOTA(inode))
  1163. return QUOTA_OK;
  1164. for (cnt = 0; cnt < MAXQUOTAS; cnt++)
  1165. warntype[cnt] = QUOTA_NL_NOWARN;
  1166. down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1167. if (IS_NOQUOTA(inode)) {
  1168. up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1169. return QUOTA_OK;
  1170. }
  1171. spin_lock(&dq_data_lock);
  1172. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1173. if (inode->i_dquot[cnt] == NODQUOT)
  1174. continue;
  1175. if (check_idq(inode->i_dquot[cnt], number, warntype+cnt) == NO_QUOTA)
  1176. goto warn_put_all;
  1177. }
  1178. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1179. if (inode->i_dquot[cnt] == NODQUOT)
  1180. continue;
  1181. dquot_incr_inodes(inode->i_dquot[cnt], number);
  1182. }
  1183. ret = QUOTA_OK;
  1184. warn_put_all:
  1185. spin_unlock(&dq_data_lock);
  1186. if (ret == QUOTA_OK)
  1187. /* Dirtify all the dquots - this can block when journalling */
  1188. for (cnt = 0; cnt < MAXQUOTAS; cnt++)
  1189. if (inode->i_dquot[cnt])
  1190. mark_dquot_dirty(inode->i_dquot[cnt]);
  1191. flush_warnings(inode->i_dquot, warntype);
  1192. up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1193. return ret;
  1194. }
  1195. /*
  1196. * This operation can block, but only after everything is updated
  1197. */
  1198. int dquot_free_space(struct inode *inode, qsize_t number)
  1199. {
  1200. unsigned int cnt;
  1201. char warntype[MAXQUOTAS];
  1202. /* First test before acquiring mutex - solves deadlocks when we
  1203. * re-enter the quota code and are already holding the mutex */
  1204. if (IS_NOQUOTA(inode)) {
  1205. out_sub:
  1206. inode_sub_bytes(inode, number);
  1207. return QUOTA_OK;
  1208. }
  1209. down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1210. /* Now recheck reliably when holding dqptr_sem */
  1211. if (IS_NOQUOTA(inode)) {
  1212. up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1213. goto out_sub;
  1214. }
  1215. spin_lock(&dq_data_lock);
  1216. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1217. if (inode->i_dquot[cnt] == NODQUOT)
  1218. continue;
  1219. warntype[cnt] = info_bdq_free(inode->i_dquot[cnt], number);
  1220. dquot_decr_space(inode->i_dquot[cnt], number);
  1221. }
  1222. inode_sub_bytes(inode, number);
  1223. spin_unlock(&dq_data_lock);
  1224. /* Dirtify all the dquots - this can block when journalling */
  1225. for (cnt = 0; cnt < MAXQUOTAS; cnt++)
  1226. if (inode->i_dquot[cnt])
  1227. mark_dquot_dirty(inode->i_dquot[cnt]);
  1228. flush_warnings(inode->i_dquot, warntype);
  1229. up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1230. return QUOTA_OK;
  1231. }
  1232. /*
  1233. * This operation can block, but only after everything is updated
  1234. */
  1235. int dquot_free_inode(const struct inode *inode, qsize_t number)
  1236. {
  1237. unsigned int cnt;
  1238. char warntype[MAXQUOTAS];
  1239. /* First test before acquiring mutex - solves deadlocks when we
  1240. * re-enter the quota code and are already holding the mutex */
  1241. if (IS_NOQUOTA(inode))
  1242. return QUOTA_OK;
  1243. down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1244. /* Now recheck reliably when holding dqptr_sem */
  1245. if (IS_NOQUOTA(inode)) {
  1246. up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1247. return QUOTA_OK;
  1248. }
  1249. spin_lock(&dq_data_lock);
  1250. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1251. if (inode->i_dquot[cnt] == NODQUOT)
  1252. continue;
  1253. warntype[cnt] = info_idq_free(inode->i_dquot[cnt], number);
  1254. dquot_decr_inodes(inode->i_dquot[cnt], number);
  1255. }
  1256. spin_unlock(&dq_data_lock);
  1257. /* Dirtify all the dquots - this can block when journalling */
  1258. for (cnt = 0; cnt < MAXQUOTAS; cnt++)
  1259. if (inode->i_dquot[cnt])
  1260. mark_dquot_dirty(inode->i_dquot[cnt]);
  1261. flush_warnings(inode->i_dquot, warntype);
  1262. up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1263. return QUOTA_OK;
  1264. }
  1265. /*
  1266. * Transfer the number of inode and blocks from one diskquota to an other.
  1267. *
  1268. * This operation can block, but only after everything is updated
  1269. * A transaction must be started when entering this function.
  1270. */
  1271. int dquot_transfer(struct inode *inode, struct iattr *iattr)
  1272. {
  1273. qsize_t space;
  1274. struct dquot *transfer_from[MAXQUOTAS];
  1275. struct dquot *transfer_to[MAXQUOTAS];
  1276. int cnt, ret = NO_QUOTA, chuid = (iattr->ia_valid & ATTR_UID) && inode->i_uid != iattr->ia_uid,
  1277. chgid = (iattr->ia_valid & ATTR_GID) && inode->i_gid != iattr->ia_gid;
  1278. char warntype_to[MAXQUOTAS];
  1279. char warntype_from_inodes[MAXQUOTAS], warntype_from_space[MAXQUOTAS];
  1280. /* First test before acquiring mutex - solves deadlocks when we
  1281. * re-enter the quota code and are already holding the mutex */
  1282. if (IS_NOQUOTA(inode))
  1283. return QUOTA_OK;
  1284. /* Clear the arrays */
  1285. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1286. transfer_to[cnt] = transfer_from[cnt] = NODQUOT;
  1287. warntype_to[cnt] = QUOTA_NL_NOWARN;
  1288. }
  1289. down_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1290. /* Now recheck reliably when holding dqptr_sem */
  1291. if (IS_NOQUOTA(inode)) { /* File without quota accounting? */
  1292. up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1293. return QUOTA_OK;
  1294. }
  1295. /* First build the transfer_to list - here we can block on
  1296. * reading/instantiating of dquots. We know that the transaction for
  1297. * us was already started so we don't violate lock ranking here */
  1298. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1299. switch (cnt) {
  1300. case USRQUOTA:
  1301. if (!chuid)
  1302. continue;
  1303. transfer_to[cnt] = dqget(inode->i_sb, iattr->ia_uid, cnt);
  1304. break;
  1305. case GRPQUOTA:
  1306. if (!chgid)
  1307. continue;
  1308. transfer_to[cnt] = dqget(inode->i_sb, iattr->ia_gid, cnt);
  1309. break;
  1310. }
  1311. }
  1312. spin_lock(&dq_data_lock);
  1313. space = inode_get_bytes(inode);
  1314. /* Build the transfer_from list and check the limits */
  1315. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1316. if (transfer_to[cnt] == NODQUOT)
  1317. continue;
  1318. transfer_from[cnt] = inode->i_dquot[cnt];
  1319. if (check_idq(transfer_to[cnt], 1, warntype_to + cnt) ==
  1320. NO_QUOTA || check_bdq(transfer_to[cnt], space, 0,
  1321. warntype_to + cnt) == NO_QUOTA)
  1322. goto warn_put_all;
  1323. }
  1324. /*
  1325. * Finally perform the needed transfer from transfer_from to transfer_to
  1326. */
  1327. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1328. /*
  1329. * Skip changes for same uid or gid or for turned off quota-type.
  1330. */
  1331. if (transfer_to[cnt] == NODQUOT)
  1332. continue;
  1333. /* Due to IO error we might not have transfer_from[] structure */
  1334. if (transfer_from[cnt]) {
  1335. warntype_from_inodes[cnt] =
  1336. info_idq_free(transfer_from[cnt], 1);
  1337. warntype_from_space[cnt] =
  1338. info_bdq_free(transfer_from[cnt], space);
  1339. dquot_decr_inodes(transfer_from[cnt], 1);
  1340. dquot_decr_space(transfer_from[cnt], space);
  1341. }
  1342. dquot_incr_inodes(transfer_to[cnt], 1);
  1343. dquot_incr_space(transfer_to[cnt], space);
  1344. inode->i_dquot[cnt] = transfer_to[cnt];
  1345. }
  1346. ret = QUOTA_OK;
  1347. warn_put_all:
  1348. spin_unlock(&dq_data_lock);
  1349. /* Dirtify all the dquots - this can block when journalling */
  1350. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1351. if (transfer_from[cnt])
  1352. mark_dquot_dirty(transfer_from[cnt]);
  1353. if (transfer_to[cnt])
  1354. mark_dquot_dirty(transfer_to[cnt]);
  1355. }
  1356. flush_warnings(transfer_to, warntype_to);
  1357. flush_warnings(transfer_from, warntype_from_inodes);
  1358. flush_warnings(transfer_from, warntype_from_space);
  1359. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1360. if (ret == QUOTA_OK && transfer_from[cnt] != NODQUOT)
  1361. dqput(transfer_from[cnt]);
  1362. if (ret == NO_QUOTA && transfer_to[cnt] != NODQUOT)
  1363. dqput(transfer_to[cnt]);
  1364. }
  1365. up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1366. return ret;
  1367. }
  1368. /* Wrapper for transferring ownership of an inode */
  1369. int vfs_dq_transfer(struct inode *inode, struct iattr *iattr)
  1370. {
  1371. if (sb_any_quota_active(inode->i_sb) && !IS_NOQUOTA(inode)) {
  1372. vfs_dq_init(inode);
  1373. if (inode->i_sb->dq_op->transfer(inode, iattr) == NO_QUOTA)
  1374. return 1;
  1375. }
  1376. return 0;
  1377. }
  1378. /*
  1379. * Write info of quota file to disk
  1380. */
  1381. int dquot_commit_info(struct super_block *sb, int type)
  1382. {
  1383. int ret;
  1384. struct quota_info *dqopt = sb_dqopt(sb);
  1385. mutex_lock(&dqopt->dqio_mutex);
  1386. ret = dqopt->ops[type]->write_file_info(sb, type);
  1387. mutex_unlock(&dqopt->dqio_mutex);
  1388. return ret;
  1389. }
  1390. /*
  1391. * Definitions of diskquota operations.
  1392. */
  1393. struct dquot_operations dquot_operations = {
  1394. .initialize = dquot_initialize,
  1395. .drop = dquot_drop,
  1396. .alloc_space = dquot_alloc_space,
  1397. .alloc_inode = dquot_alloc_inode,
  1398. .free_space = dquot_free_space,
  1399. .free_inode = dquot_free_inode,
  1400. .transfer = dquot_transfer,
  1401. .write_dquot = dquot_commit,
  1402. .acquire_dquot = dquot_acquire,
  1403. .release_dquot = dquot_release,
  1404. .mark_dirty = dquot_mark_dquot_dirty,
  1405. .write_info = dquot_commit_info,
  1406. .alloc_dquot = dquot_alloc,
  1407. .destroy_dquot = dquot_destroy,
  1408. };
  1409. /*
  1410. * Turn quota off on a device. type == -1 ==> quotaoff for all types (umount)
  1411. */
  1412. int vfs_quota_disable(struct super_block *sb, int type, unsigned int flags)
  1413. {
  1414. int cnt, ret = 0;
  1415. struct quota_info *dqopt = sb_dqopt(sb);
  1416. struct inode *toputinode[MAXQUOTAS];
  1417. /* Cannot turn off usage accounting without turning off limits, or
  1418. * suspend quotas and simultaneously turn quotas off. */
  1419. if ((flags & DQUOT_USAGE_ENABLED && !(flags & DQUOT_LIMITS_ENABLED))
  1420. || (flags & DQUOT_SUSPENDED && flags & (DQUOT_LIMITS_ENABLED |
  1421. DQUOT_USAGE_ENABLED)))
  1422. return -EINVAL;
  1423. /* We need to serialize quota_off() for device */
  1424. mutex_lock(&dqopt->dqonoff_mutex);
  1425. /*
  1426. * Skip everything if there's nothing to do. We have to do this because
  1427. * sometimes we are called when fill_super() failed and calling
  1428. * sync_fs() in such cases does no good.
  1429. */
  1430. if (!sb_any_quota_loaded(sb)) {
  1431. mutex_unlock(&dqopt->dqonoff_mutex);
  1432. return 0;
  1433. }
  1434. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1435. toputinode[cnt] = NULL;
  1436. if (type != -1 && cnt != type)
  1437. continue;
  1438. if (!sb_has_quota_loaded(sb, cnt))
  1439. continue;
  1440. if (flags & DQUOT_SUSPENDED) {
  1441. dqopt->flags |=
  1442. dquot_state_flag(DQUOT_SUSPENDED, cnt);
  1443. } else {
  1444. dqopt->flags &= ~dquot_state_flag(flags, cnt);
  1445. /* Turning off suspended quotas? */
  1446. if (!sb_has_quota_loaded(sb, cnt) &&
  1447. sb_has_quota_suspended(sb, cnt)) {
  1448. dqopt->flags &= ~dquot_state_flag(
  1449. DQUOT_SUSPENDED, cnt);
  1450. iput(dqopt->files[cnt]);
  1451. dqopt->files[cnt] = NULL;
  1452. continue;
  1453. }
  1454. }
  1455. /* We still have to keep quota loaded? */
  1456. if (sb_has_quota_loaded(sb, cnt) && !(flags & DQUOT_SUSPENDED))
  1457. continue;
  1458. /* Note: these are blocking operations */
  1459. drop_dquot_ref(sb, cnt);
  1460. invalidate_dquots(sb, cnt);
  1461. /*
  1462. * Now all dquots should be invalidated, all writes done so we should be only
  1463. * users of the info. No locks needed.
  1464. */
  1465. if (info_dirty(&dqopt->info[cnt]))
  1466. sb->dq_op->write_info(sb, cnt);
  1467. if (dqopt->ops[cnt]->free_file_info)
  1468. dqopt->ops[cnt]->free_file_info(sb, cnt);
  1469. put_quota_format(dqopt->info[cnt].dqi_format);
  1470. toputinode[cnt] = dqopt->files[cnt];
  1471. if (!sb_has_quota_loaded(sb, cnt))
  1472. dqopt->files[cnt] = NULL;
  1473. dqopt->info[cnt].dqi_flags = 0;
  1474. dqopt->info[cnt].dqi_igrace = 0;
  1475. dqopt->info[cnt].dqi_bgrace = 0;
  1476. dqopt->ops[cnt] = NULL;
  1477. }
  1478. mutex_unlock(&dqopt->dqonoff_mutex);
  1479. /* Sync the superblock so that buffers with quota data are written to
  1480. * disk (and so userspace sees correct data afterwards). */
  1481. if (sb->s_op->sync_fs)
  1482. sb->s_op->sync_fs(sb, 1);
  1483. sync_blockdev(sb->s_bdev);
  1484. /* Now the quota files are just ordinary files and we can set the
  1485. * inode flags back. Moreover we discard the pagecache so that
  1486. * userspace sees the writes we did bypassing the pagecache. We
  1487. * must also discard the blockdev buffers so that we see the
  1488. * changes done by userspace on the next quotaon() */
  1489. for (cnt = 0; cnt < MAXQUOTAS; cnt++)
  1490. if (toputinode[cnt]) {
  1491. mutex_lock(&dqopt->dqonoff_mutex);
  1492. /* If quota was reenabled in the meantime, we have
  1493. * nothing to do */
  1494. if (!sb_has_quota_loaded(sb, cnt)) {
  1495. mutex_lock_nested(&toputinode[cnt]->i_mutex, I_MUTEX_QUOTA);
  1496. toputinode[cnt]->i_flags &= ~(S_IMMUTABLE |
  1497. S_NOATIME | S_NOQUOTA);
  1498. truncate_inode_pages(&toputinode[cnt]->i_data, 0);
  1499. mutex_unlock(&toputinode[cnt]->i_mutex);
  1500. mark_inode_dirty(toputinode[cnt]);
  1501. }
  1502. mutex_unlock(&dqopt->dqonoff_mutex);
  1503. /* On remount RO, we keep the inode pointer so that we
  1504. * can reenable quota on the subsequent remount RW. We
  1505. * have to check 'flags' variable and not use sb_has_
  1506. * function because another quotaon / quotaoff could
  1507. * change global state before we got here. We refuse
  1508. * to suspend quotas when there is pending delete on
  1509. * the quota file... */
  1510. if (!(flags & DQUOT_SUSPENDED))
  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. int vfs_quota_off(struct super_block *sb, int type, int remount)
  1520. {
  1521. return vfs_quota_disable(sb, type, remount ? DQUOT_SUSPENDED :
  1522. (DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED));
  1523. }
  1524. /*
  1525. * Turn quotas on on a device
  1526. */
  1527. /*
  1528. * Helper function to turn quotas on when we already have the inode of
  1529. * quota file and no quota information is loaded.
  1530. */
  1531. static int vfs_load_quota_inode(struct inode *inode, int type, int format_id,
  1532. unsigned int flags)
  1533. {
  1534. struct quota_format_type *fmt = find_quota_format(format_id);
  1535. struct super_block *sb = inode->i_sb;
  1536. struct quota_info *dqopt = sb_dqopt(sb);
  1537. int error;
  1538. int oldflags = -1;
  1539. if (!fmt)
  1540. return -ESRCH;
  1541. if (!S_ISREG(inode->i_mode)) {
  1542. error = -EACCES;
  1543. goto out_fmt;
  1544. }
  1545. if (IS_RDONLY(inode)) {
  1546. error = -EROFS;
  1547. goto out_fmt;
  1548. }
  1549. if (!sb->s_op->quota_write || !sb->s_op->quota_read) {
  1550. error = -EINVAL;
  1551. goto out_fmt;
  1552. }
  1553. /* Usage always has to be set... */
  1554. if (!(flags & DQUOT_USAGE_ENABLED)) {
  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_loaded(sb, type)) {
  1566. error = -EBUSY;
  1567. goto out_lock;
  1568. }
  1569. /* We don't want quota and atime on quota files (deadlocks possible)
  1570. * Also nobody should write to the file - we use special IO operations
  1571. * which ignore the immutable bit. */
  1572. down_write(&dqopt->dqptr_sem);
  1573. oldflags = inode->i_flags & (S_NOATIME | S_IMMUTABLE | S_NOQUOTA);
  1574. inode->i_flags |= S_NOQUOTA | S_NOATIME | S_IMMUTABLE;
  1575. up_write(&dqopt->dqptr_sem);
  1576. sb->dq_op->drop(inode);
  1577. error = -EIO;
  1578. dqopt->files[type] = igrab(inode);
  1579. if (!dqopt->files[type])
  1580. goto out_lock;
  1581. error = -EINVAL;
  1582. if (!fmt->qf_ops->check_quota_file(sb, type))
  1583. goto out_file_init;
  1584. dqopt->ops[type] = fmt->qf_ops;
  1585. dqopt->info[type].dqi_format = fmt;
  1586. dqopt->info[type].dqi_fmt_id = format_id;
  1587. INIT_LIST_HEAD(&dqopt->info[type].dqi_dirty_list);
  1588. mutex_lock(&dqopt->dqio_mutex);
  1589. if ((error = dqopt->ops[type]->read_file_info(sb, type)) < 0) {
  1590. mutex_unlock(&dqopt->dqio_mutex);
  1591. goto out_file_init;
  1592. }
  1593. mutex_unlock(&dqopt->dqio_mutex);
  1594. mutex_unlock(&inode->i_mutex);
  1595. dqopt->flags |= dquot_state_flag(flags, type);
  1596. add_dquot_ref(sb, type);
  1597. mutex_unlock(&dqopt->dqonoff_mutex);
  1598. return 0;
  1599. out_file_init:
  1600. dqopt->files[type] = NULL;
  1601. iput(inode);
  1602. out_lock:
  1603. mutex_unlock(&dqopt->dqonoff_mutex);
  1604. if (oldflags != -1) {
  1605. down_write(&dqopt->dqptr_sem);
  1606. /* Set the flags back (in the case of accidental quotaon()
  1607. * on a wrong file we don't want to mess up the flags) */
  1608. inode->i_flags &= ~(S_NOATIME | S_NOQUOTA | S_IMMUTABLE);
  1609. inode->i_flags |= oldflags;
  1610. up_write(&dqopt->dqptr_sem);
  1611. }
  1612. mutex_unlock(&inode->i_mutex);
  1613. out_fmt:
  1614. put_quota_format(fmt);
  1615. return error;
  1616. }
  1617. /* Reenable quotas on remount RW */
  1618. static int vfs_quota_on_remount(struct super_block *sb, int type)
  1619. {
  1620. struct quota_info *dqopt = sb_dqopt(sb);
  1621. struct inode *inode;
  1622. int ret;
  1623. unsigned int flags;
  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. inode = dqopt->files[type];
  1630. dqopt->files[type] = NULL;
  1631. flags = dqopt->flags & dquot_state_flag(DQUOT_USAGE_ENABLED |
  1632. DQUOT_LIMITS_ENABLED, type);
  1633. dqopt->flags &= ~dquot_state_flag(DQUOT_STATE_FLAGS, type);
  1634. mutex_unlock(&dqopt->dqonoff_mutex);
  1635. flags = dquot_generic_flag(flags, type);
  1636. ret = vfs_load_quota_inode(inode, type, dqopt->info[type].dqi_fmt_id,
  1637. flags);
  1638. iput(inode);
  1639. return ret;
  1640. }
  1641. int vfs_quota_on_path(struct super_block *sb, int type, int format_id,
  1642. struct path *path)
  1643. {
  1644. int error = security_quota_on(path->dentry);
  1645. if (error)
  1646. return error;
  1647. /* Quota file not on the same filesystem? */
  1648. if (path->mnt->mnt_sb != sb)
  1649. error = -EXDEV;
  1650. else
  1651. error = vfs_load_quota_inode(path->dentry->d_inode, type,
  1652. format_id, DQUOT_USAGE_ENABLED |
  1653. DQUOT_LIMITS_ENABLED);
  1654. return error;
  1655. }
  1656. int vfs_quota_on(struct super_block *sb, int type, int format_id, char *name,
  1657. int remount)
  1658. {
  1659. struct path path;
  1660. int error;
  1661. if (remount)
  1662. return vfs_quota_on_remount(sb, type);
  1663. error = kern_path(name, LOOKUP_FOLLOW, &path);
  1664. if (!error) {
  1665. error = vfs_quota_on_path(sb, type, format_id, &path);
  1666. path_put(&path);
  1667. }
  1668. return error;
  1669. }
  1670. /*
  1671. * More powerful function for turning on quotas allowing setting
  1672. * of individual quota flags
  1673. */
  1674. int vfs_quota_enable(struct inode *inode, int type, int format_id,
  1675. unsigned int flags)
  1676. {
  1677. int ret = 0;
  1678. struct super_block *sb = inode->i_sb;
  1679. struct quota_info *dqopt = sb_dqopt(sb);
  1680. /* Just unsuspend quotas? */
  1681. if (flags & DQUOT_SUSPENDED)
  1682. return vfs_quota_on_remount(sb, type);
  1683. if (!flags)
  1684. return 0;
  1685. /* Just updating flags needed? */
  1686. if (sb_has_quota_loaded(sb, type)) {
  1687. mutex_lock(&dqopt->dqonoff_mutex);
  1688. /* Now do a reliable test... */
  1689. if (!sb_has_quota_loaded(sb, type)) {
  1690. mutex_unlock(&dqopt->dqonoff_mutex);
  1691. goto load_quota;
  1692. }
  1693. if (flags & DQUOT_USAGE_ENABLED &&
  1694. sb_has_quota_usage_enabled(sb, type)) {
  1695. ret = -EBUSY;
  1696. goto out_lock;
  1697. }
  1698. if (flags & DQUOT_LIMITS_ENABLED &&
  1699. sb_has_quota_limits_enabled(sb, type)) {
  1700. ret = -EBUSY;
  1701. goto out_lock;
  1702. }
  1703. sb_dqopt(sb)->flags |= dquot_state_flag(flags, type);
  1704. out_lock:
  1705. mutex_unlock(&dqopt->dqonoff_mutex);
  1706. return ret;
  1707. }
  1708. load_quota:
  1709. return vfs_load_quota_inode(inode, type, format_id, flags);
  1710. }
  1711. /*
  1712. * This function is used when filesystem needs to initialize quotas
  1713. * during mount time.
  1714. */
  1715. int vfs_quota_on_mount(struct super_block *sb, char *qf_name,
  1716. int format_id, int type)
  1717. {
  1718. struct dentry *dentry;
  1719. int error;
  1720. dentry = lookup_one_len(qf_name, sb->s_root, strlen(qf_name));
  1721. if (IS_ERR(dentry))
  1722. return PTR_ERR(dentry);
  1723. if (!dentry->d_inode) {
  1724. error = -ENOENT;
  1725. goto out;
  1726. }
  1727. error = security_quota_on(dentry);
  1728. if (!error)
  1729. error = vfs_load_quota_inode(dentry->d_inode, type, format_id,
  1730. DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED);
  1731. out:
  1732. dput(dentry);
  1733. return error;
  1734. }
  1735. /* Wrapper to turn on quotas when remounting rw */
  1736. int vfs_dq_quota_on_remount(struct super_block *sb)
  1737. {
  1738. int cnt;
  1739. int ret = 0, err;
  1740. if (!sb->s_qcop || !sb->s_qcop->quota_on)
  1741. return -ENOSYS;
  1742. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1743. err = sb->s_qcop->quota_on(sb, cnt, 0, NULL, 1);
  1744. if (err < 0 && !ret)
  1745. ret = err;
  1746. }
  1747. return ret;
  1748. }
  1749. static inline qsize_t qbtos(qsize_t blocks)
  1750. {
  1751. return blocks << QIF_DQBLKSIZE_BITS;
  1752. }
  1753. static inline qsize_t stoqb(qsize_t space)
  1754. {
  1755. return (space + QIF_DQBLKSIZE - 1) >> QIF_DQBLKSIZE_BITS;
  1756. }
  1757. /* Generic routine for getting common part of quota structure */
  1758. static void do_get_dqblk(struct dquot *dquot, struct if_dqblk *di)
  1759. {
  1760. struct mem_dqblk *dm = &dquot->dq_dqb;
  1761. spin_lock(&dq_data_lock);
  1762. di->dqb_bhardlimit = stoqb(dm->dqb_bhardlimit);
  1763. di->dqb_bsoftlimit = stoqb(dm->dqb_bsoftlimit);
  1764. di->dqb_curspace = dm->dqb_curspace;
  1765. di->dqb_ihardlimit = dm->dqb_ihardlimit;
  1766. di->dqb_isoftlimit = dm->dqb_isoftlimit;
  1767. di->dqb_curinodes = dm->dqb_curinodes;
  1768. di->dqb_btime = dm->dqb_btime;
  1769. di->dqb_itime = dm->dqb_itime;
  1770. di->dqb_valid = QIF_ALL;
  1771. spin_unlock(&dq_data_lock);
  1772. }
  1773. int vfs_get_dqblk(struct super_block *sb, int type, qid_t id, struct if_dqblk *di)
  1774. {
  1775. struct dquot *dquot;
  1776. mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
  1777. if (!(dquot = dqget(sb, id, type))) {
  1778. mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
  1779. return -ESRCH;
  1780. }
  1781. do_get_dqblk(dquot, di);
  1782. dqput(dquot);
  1783. mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
  1784. return 0;
  1785. }
  1786. /* Generic routine for setting common part of quota structure */
  1787. static int do_set_dqblk(struct dquot *dquot, struct if_dqblk *di)
  1788. {
  1789. struct mem_dqblk *dm = &dquot->dq_dqb;
  1790. int check_blim = 0, check_ilim = 0;
  1791. struct mem_dqinfo *dqi = &sb_dqopt(dquot->dq_sb)->info[dquot->dq_type];
  1792. if ((di->dqb_valid & QIF_BLIMITS &&
  1793. (di->dqb_bhardlimit > dqi->dqi_maxblimit ||
  1794. di->dqb_bsoftlimit > dqi->dqi_maxblimit)) ||
  1795. (di->dqb_valid & QIF_ILIMITS &&
  1796. (di->dqb_ihardlimit > dqi->dqi_maxilimit ||
  1797. di->dqb_isoftlimit > dqi->dqi_maxilimit)))
  1798. return -ERANGE;
  1799. spin_lock(&dq_data_lock);
  1800. if (di->dqb_valid & QIF_SPACE) {
  1801. dm->dqb_curspace = di->dqb_curspace;
  1802. check_blim = 1;
  1803. }
  1804. if (di->dqb_valid & QIF_BLIMITS) {
  1805. dm->dqb_bsoftlimit = qbtos(di->dqb_bsoftlimit);
  1806. dm->dqb_bhardlimit = qbtos(di->dqb_bhardlimit);
  1807. check_blim = 1;
  1808. }
  1809. if (di->dqb_valid & QIF_INODES) {
  1810. dm->dqb_curinodes = di->dqb_curinodes;
  1811. check_ilim = 1;
  1812. }
  1813. if (di->dqb_valid & QIF_ILIMITS) {
  1814. dm->dqb_isoftlimit = di->dqb_isoftlimit;
  1815. dm->dqb_ihardlimit = di->dqb_ihardlimit;
  1816. check_ilim = 1;
  1817. }
  1818. if (di->dqb_valid & QIF_BTIME)
  1819. dm->dqb_btime = di->dqb_btime;
  1820. if (di->dqb_valid & QIF_ITIME)
  1821. dm->dqb_itime = di->dqb_itime;
  1822. if (check_blim) {
  1823. if (!dm->dqb_bsoftlimit || dm->dqb_curspace < dm->dqb_bsoftlimit) {
  1824. dm->dqb_btime = 0;
  1825. clear_bit(DQ_BLKS_B, &dquot->dq_flags);
  1826. }
  1827. else if (!(di->dqb_valid & QIF_BTIME)) /* Set grace only if user hasn't provided his own... */
  1828. dm->dqb_btime = get_seconds() + dqi->dqi_bgrace;
  1829. }
  1830. if (check_ilim) {
  1831. if (!dm->dqb_isoftlimit || dm->dqb_curinodes < dm->dqb_isoftlimit) {
  1832. dm->dqb_itime = 0;
  1833. clear_bit(DQ_INODES_B, &dquot->dq_flags);
  1834. }
  1835. else if (!(di->dqb_valid & QIF_ITIME)) /* Set grace only if user hasn't provided his own... */
  1836. dm->dqb_itime = get_seconds() + dqi->dqi_igrace;
  1837. }
  1838. if (dm->dqb_bhardlimit || dm->dqb_bsoftlimit || dm->dqb_ihardlimit || dm->dqb_isoftlimit)
  1839. clear_bit(DQ_FAKE_B, &dquot->dq_flags);
  1840. else
  1841. set_bit(DQ_FAKE_B, &dquot->dq_flags);
  1842. spin_unlock(&dq_data_lock);
  1843. mark_dquot_dirty(dquot);
  1844. return 0;
  1845. }
  1846. int vfs_set_dqblk(struct super_block *sb, int type, qid_t id, struct if_dqblk *di)
  1847. {
  1848. struct dquot *dquot;
  1849. int rc;
  1850. mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
  1851. dquot = dqget(sb, id, type);
  1852. if (!dquot) {
  1853. rc = -ESRCH;
  1854. goto out;
  1855. }
  1856. rc = do_set_dqblk(dquot, di);
  1857. dqput(dquot);
  1858. out:
  1859. mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
  1860. return rc;
  1861. }
  1862. /* Generic routine for getting common part of quota file information */
  1863. int vfs_get_dqinfo(struct super_block *sb, int type, struct if_dqinfo *ii)
  1864. {
  1865. struct mem_dqinfo *mi;
  1866. mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
  1867. if (!sb_has_quota_active(sb, type)) {
  1868. mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
  1869. return -ESRCH;
  1870. }
  1871. mi = sb_dqopt(sb)->info + type;
  1872. spin_lock(&dq_data_lock);
  1873. ii->dqi_bgrace = mi->dqi_bgrace;
  1874. ii->dqi_igrace = mi->dqi_igrace;
  1875. ii->dqi_flags = mi->dqi_flags & DQF_MASK;
  1876. ii->dqi_valid = IIF_ALL;
  1877. spin_unlock(&dq_data_lock);
  1878. mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
  1879. return 0;
  1880. }
  1881. /* Generic routine for setting common part of quota file information */
  1882. int vfs_set_dqinfo(struct super_block *sb, int type, struct if_dqinfo *ii)
  1883. {
  1884. struct mem_dqinfo *mi;
  1885. int err = 0;
  1886. mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
  1887. if (!sb_has_quota_active(sb, type)) {
  1888. err = -ESRCH;
  1889. goto out;
  1890. }
  1891. mi = sb_dqopt(sb)->info + type;
  1892. spin_lock(&dq_data_lock);
  1893. if (ii->dqi_valid & IIF_BGRACE)
  1894. mi->dqi_bgrace = ii->dqi_bgrace;
  1895. if (ii->dqi_valid & IIF_IGRACE)
  1896. mi->dqi_igrace = ii->dqi_igrace;
  1897. if (ii->dqi_valid & IIF_FLAGS)
  1898. mi->dqi_flags = (mi->dqi_flags & ~DQF_MASK) | (ii->dqi_flags & DQF_MASK);
  1899. spin_unlock(&dq_data_lock);
  1900. mark_info_dirty(sb, type);
  1901. /* Force write to disk */
  1902. sb->dq_op->write_info(sb, type);
  1903. out:
  1904. mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
  1905. return err;
  1906. }
  1907. struct quotactl_ops vfs_quotactl_ops = {
  1908. .quota_on = vfs_quota_on,
  1909. .quota_off = vfs_quota_off,
  1910. .quota_sync = vfs_quota_sync,
  1911. .get_info = vfs_get_dqinfo,
  1912. .set_info = vfs_set_dqinfo,
  1913. .get_dqblk = vfs_get_dqblk,
  1914. .set_dqblk = vfs_set_dqblk
  1915. };
  1916. static ctl_table fs_dqstats_table[] = {
  1917. {
  1918. .ctl_name = FS_DQ_LOOKUPS,
  1919. .procname = "lookups",
  1920. .data = &dqstats.lookups,
  1921. .maxlen = sizeof(int),
  1922. .mode = 0444,
  1923. .proc_handler = &proc_dointvec,
  1924. },
  1925. {
  1926. .ctl_name = FS_DQ_DROPS,
  1927. .procname = "drops",
  1928. .data = &dqstats.drops,
  1929. .maxlen = sizeof(int),
  1930. .mode = 0444,
  1931. .proc_handler = &proc_dointvec,
  1932. },
  1933. {
  1934. .ctl_name = FS_DQ_READS,
  1935. .procname = "reads",
  1936. .data = &dqstats.reads,
  1937. .maxlen = sizeof(int),
  1938. .mode = 0444,
  1939. .proc_handler = &proc_dointvec,
  1940. },
  1941. {
  1942. .ctl_name = FS_DQ_WRITES,
  1943. .procname = "writes",
  1944. .data = &dqstats.writes,
  1945. .maxlen = sizeof(int),
  1946. .mode = 0444,
  1947. .proc_handler = &proc_dointvec,
  1948. },
  1949. {
  1950. .ctl_name = FS_DQ_CACHE_HITS,
  1951. .procname = "cache_hits",
  1952. .data = &dqstats.cache_hits,
  1953. .maxlen = sizeof(int),
  1954. .mode = 0444,
  1955. .proc_handler = &proc_dointvec,
  1956. },
  1957. {
  1958. .ctl_name = FS_DQ_ALLOCATED,
  1959. .procname = "allocated_dquots",
  1960. .data = &dqstats.allocated_dquots,
  1961. .maxlen = sizeof(int),
  1962. .mode = 0444,
  1963. .proc_handler = &proc_dointvec,
  1964. },
  1965. {
  1966. .ctl_name = FS_DQ_FREE,
  1967. .procname = "free_dquots",
  1968. .data = &dqstats.free_dquots,
  1969. .maxlen = sizeof(int),
  1970. .mode = 0444,
  1971. .proc_handler = &proc_dointvec,
  1972. },
  1973. {
  1974. .ctl_name = FS_DQ_SYNCS,
  1975. .procname = "syncs",
  1976. .data = &dqstats.syncs,
  1977. .maxlen = sizeof(int),
  1978. .mode = 0444,
  1979. .proc_handler = &proc_dointvec,
  1980. },
  1981. #ifdef CONFIG_PRINT_QUOTA_WARNING
  1982. {
  1983. .ctl_name = FS_DQ_WARNINGS,
  1984. .procname = "warnings",
  1985. .data = &flag_print_warnings,
  1986. .maxlen = sizeof(int),
  1987. .mode = 0644,
  1988. .proc_handler = &proc_dointvec,
  1989. },
  1990. #endif
  1991. { .ctl_name = 0 },
  1992. };
  1993. static ctl_table fs_table[] = {
  1994. {
  1995. .ctl_name = FS_DQSTATS,
  1996. .procname = "quota",
  1997. .mode = 0555,
  1998. .child = fs_dqstats_table,
  1999. },
  2000. { .ctl_name = 0 },
  2001. };
  2002. static ctl_table sys_table[] = {
  2003. {
  2004. .ctl_name = CTL_FS,
  2005. .procname = "fs",
  2006. .mode = 0555,
  2007. .child = fs_table,
  2008. },
  2009. { .ctl_name = 0 },
  2010. };
  2011. static int __init dquot_init(void)
  2012. {
  2013. int i;
  2014. unsigned long nr_hash, order;
  2015. printk(KERN_NOTICE "VFS: Disk quotas %s\n", __DQUOT_VERSION__);
  2016. register_sysctl_table(sys_table);
  2017. dquot_cachep = kmem_cache_create("dquot",
  2018. sizeof(struct dquot), sizeof(unsigned long) * 4,
  2019. (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
  2020. SLAB_MEM_SPREAD|SLAB_PANIC),
  2021. NULL);
  2022. order = 0;
  2023. dquot_hash = (struct hlist_head *)__get_free_pages(GFP_ATOMIC, order);
  2024. if (!dquot_hash)
  2025. panic("Cannot create dquot hash table");
  2026. /* Find power-of-two hlist_heads which can fit into allocation */
  2027. nr_hash = (1UL << order) * PAGE_SIZE / sizeof(struct hlist_head);
  2028. dq_hash_bits = 0;
  2029. do {
  2030. dq_hash_bits++;
  2031. } while (nr_hash >> dq_hash_bits);
  2032. dq_hash_bits--;
  2033. nr_hash = 1UL << dq_hash_bits;
  2034. dq_hash_mask = nr_hash - 1;
  2035. for (i = 0; i < nr_hash; i++)
  2036. INIT_HLIST_HEAD(dquot_hash + i);
  2037. printk("Dquot-cache hash table entries: %ld (order %ld, %ld bytes)\n",
  2038. nr_hash, order, (PAGE_SIZE << order));
  2039. register_shrinker(&dqcache_shrinker);
  2040. #ifdef CONFIG_QUOTA_NETLINK_INTERFACE
  2041. if (genl_register_family(&quota_genl_family) != 0)
  2042. printk(KERN_ERR "VFS: Failed to create quota netlink interface.\n");
  2043. #endif
  2044. return 0;
  2045. }
  2046. module_init(dquot_init);
  2047. EXPORT_SYMBOL(register_quota_format);
  2048. EXPORT_SYMBOL(unregister_quota_format);
  2049. EXPORT_SYMBOL(dqstats);
  2050. EXPORT_SYMBOL(dq_data_lock);
  2051. EXPORT_SYMBOL(vfs_quota_enable);
  2052. EXPORT_SYMBOL(vfs_quota_on);
  2053. EXPORT_SYMBOL(vfs_quota_on_path);
  2054. EXPORT_SYMBOL(vfs_quota_on_mount);
  2055. EXPORT_SYMBOL(vfs_quota_disable);
  2056. EXPORT_SYMBOL(vfs_quota_off);
  2057. EXPORT_SYMBOL(vfs_quota_sync);
  2058. EXPORT_SYMBOL(vfs_get_dqinfo);
  2059. EXPORT_SYMBOL(vfs_set_dqinfo);
  2060. EXPORT_SYMBOL(vfs_get_dqblk);
  2061. EXPORT_SYMBOL(vfs_set_dqblk);
  2062. EXPORT_SYMBOL(dquot_commit);
  2063. EXPORT_SYMBOL(dquot_commit_info);
  2064. EXPORT_SYMBOL(dquot_acquire);
  2065. EXPORT_SYMBOL(dquot_release);
  2066. EXPORT_SYMBOL(dquot_mark_dquot_dirty);
  2067. EXPORT_SYMBOL(dquot_initialize);
  2068. EXPORT_SYMBOL(dquot_drop);
  2069. EXPORT_SYMBOL(vfs_dq_drop);
  2070. EXPORT_SYMBOL(dquot_alloc_space);
  2071. EXPORT_SYMBOL(dquot_alloc_inode);
  2072. EXPORT_SYMBOL(dquot_free_space);
  2073. EXPORT_SYMBOL(dquot_free_inode);
  2074. EXPORT_SYMBOL(dquot_transfer);
  2075. EXPORT_SYMBOL(vfs_dq_transfer);
  2076. EXPORT_SYMBOL(vfs_dq_quota_on_remount);