dquot.c 66 KB

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