dquot.c 72 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662
  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 "../internal.h" /* ugh */
  79. #include <asm/uaccess.h>
  80. /*
  81. * There are three quota SMP locks. dq_list_lock protects all lists with quotas
  82. * and quota formats.
  83. * dq_data_lock protects data from dq_dqb and also mem_dqinfo structures and
  84. * also guards consistency of dquot->dq_dqb with inode->i_blocks, i_bytes.
  85. * i_blocks and i_bytes updates itself are guarded by i_lock acquired directly
  86. * in inode_add_bytes() and inode_sub_bytes(). dq_state_lock protects
  87. * modifications of quota state (on quotaon and quotaoff) and readers who care
  88. * about latest values take it as well.
  89. *
  90. * The spinlock ordering is hence: dq_data_lock > dq_list_lock > i_lock,
  91. * dq_list_lock > dq_state_lock
  92. *
  93. * Note that some things (eg. sb pointer, type, id) doesn't change during
  94. * the life of the dquot structure and so needn't to be protected by a lock
  95. *
  96. * Any operation working on dquots via inode pointers must hold dqptr_sem. If
  97. * operation is just reading pointers from inode (or not using them at all) the
  98. * read lock is enough. If pointers are altered function must hold write lock.
  99. * Special care needs to be taken about S_NOQUOTA inode flag (marking that
  100. * inode is a quota file). Functions adding pointers from inode to dquots have
  101. * to check this flag under dqptr_sem and then (if S_NOQUOTA is not set) they
  102. * have to do all pointer modifications before dropping dqptr_sem. This makes
  103. * sure they cannot race with quotaon which first sets S_NOQUOTA flag and
  104. * then drops all pointers to dquots from an inode.
  105. *
  106. * Each dquot has its dq_lock mutex. Locked dquots might not be referenced
  107. * from inodes (dquot_alloc_space() and such don't check the dq_lock).
  108. * Currently dquot is locked only when it is being read to memory (or space for
  109. * it is being allocated) on the first dqget() and when it is being released on
  110. * the last dqput(). The allocation and release oparations are serialized by
  111. * the dq_lock and by checking the use count in dquot_release(). Write
  112. * operations on dquots don't hold dq_lock as they copy data under dq_data_lock
  113. * spinlock to internal buffers before writing.
  114. *
  115. * Lock ordering (including related VFS locks) is the following:
  116. * i_mutex > dqonoff_sem > journal_lock > dqptr_sem > dquot->dq_lock >
  117. * dqio_mutex
  118. * The lock ordering of dqptr_sem imposed by quota code is only dqonoff_sem >
  119. * dqptr_sem. But filesystem has to count with the fact that functions such as
  120. * dquot_alloc_space() acquire dqptr_sem and they usually have to be called
  121. * from inside a transaction to keep filesystem consistency after a crash. Also
  122. * filesystems usually want to do some IO on dquot from ->mark_dirty which is
  123. * called with dqptr_sem held.
  124. * i_mutex on quota files is special (it's below dqio_mutex)
  125. */
  126. static __cacheline_aligned_in_smp DEFINE_SPINLOCK(dq_list_lock);
  127. static __cacheline_aligned_in_smp DEFINE_SPINLOCK(dq_state_lock);
  128. __cacheline_aligned_in_smp DEFINE_SPINLOCK(dq_data_lock);
  129. EXPORT_SYMBOL(dq_data_lock);
  130. void __quota_error(struct super_block *sb, const char *func,
  131. const char *fmt, ...)
  132. {
  133. if (printk_ratelimit()) {
  134. va_list args;
  135. struct va_format vaf;
  136. va_start(args, fmt);
  137. vaf.fmt = fmt;
  138. vaf.va = &args;
  139. printk(KERN_ERR "Quota error (device %s): %s: %pV\n",
  140. sb->s_id, func, &vaf);
  141. va_end(args);
  142. }
  143. }
  144. EXPORT_SYMBOL(__quota_error);
  145. #if defined(CONFIG_QUOTA_DEBUG) || defined(CONFIG_PRINT_QUOTA_WARNING)
  146. static char *quotatypes[] = INITQFNAMES;
  147. #endif
  148. static struct quota_format_type *quota_formats; /* List of registered formats */
  149. static struct quota_module_name module_names[] = INIT_QUOTA_MODULE_NAMES;
  150. /* SLAB cache for dquot structures */
  151. static struct kmem_cache *dquot_cachep;
  152. int register_quota_format(struct quota_format_type *fmt)
  153. {
  154. spin_lock(&dq_list_lock);
  155. fmt->qf_next = quota_formats;
  156. quota_formats = fmt;
  157. spin_unlock(&dq_list_lock);
  158. return 0;
  159. }
  160. EXPORT_SYMBOL(register_quota_format);
  161. void unregister_quota_format(struct quota_format_type *fmt)
  162. {
  163. struct quota_format_type **actqf;
  164. spin_lock(&dq_list_lock);
  165. for (actqf = &quota_formats; *actqf && *actqf != fmt;
  166. actqf = &(*actqf)->qf_next)
  167. ;
  168. if (*actqf)
  169. *actqf = (*actqf)->qf_next;
  170. spin_unlock(&dq_list_lock);
  171. }
  172. EXPORT_SYMBOL(unregister_quota_format);
  173. static struct quota_format_type *find_quota_format(int id)
  174. {
  175. struct quota_format_type *actqf;
  176. spin_lock(&dq_list_lock);
  177. for (actqf = quota_formats; actqf && actqf->qf_fmt_id != id;
  178. actqf = actqf->qf_next)
  179. ;
  180. if (!actqf || !try_module_get(actqf->qf_owner)) {
  181. int qm;
  182. spin_unlock(&dq_list_lock);
  183. for (qm = 0; module_names[qm].qm_fmt_id &&
  184. module_names[qm].qm_fmt_id != id; qm++)
  185. ;
  186. if (!module_names[qm].qm_fmt_id ||
  187. request_module(module_names[qm].qm_mod_name))
  188. return NULL;
  189. spin_lock(&dq_list_lock);
  190. for (actqf = quota_formats; actqf && actqf->qf_fmt_id != id;
  191. actqf = actqf->qf_next)
  192. ;
  193. if (actqf && !try_module_get(actqf->qf_owner))
  194. actqf = NULL;
  195. }
  196. spin_unlock(&dq_list_lock);
  197. return actqf;
  198. }
  199. static void put_quota_format(struct quota_format_type *fmt)
  200. {
  201. module_put(fmt->qf_owner);
  202. }
  203. /*
  204. * Dquot List Management:
  205. * The quota code uses three lists for dquot management: the inuse_list,
  206. * free_dquots, and dquot_hash[] array. A single dquot structure may be
  207. * on all three lists, depending on its current state.
  208. *
  209. * All dquots are placed to the end of inuse_list when first created, and this
  210. * list is used for invalidate operation, which must look at every dquot.
  211. *
  212. * Unused dquots (dq_count == 0) are added to the free_dquots list when freed,
  213. * and this list is searched whenever we need an available dquot. Dquots are
  214. * removed from the list as soon as they are used again, and
  215. * dqstats.free_dquots gives the number of dquots on the list. When
  216. * dquot is invalidated it's completely released from memory.
  217. *
  218. * Dquots with a specific identity (device, type and id) are placed on
  219. * one of the dquot_hash[] hash chains. The provides an efficient search
  220. * mechanism to locate a specific dquot.
  221. */
  222. static LIST_HEAD(inuse_list);
  223. static LIST_HEAD(free_dquots);
  224. static unsigned int dq_hash_bits, dq_hash_mask;
  225. static struct hlist_head *dquot_hash;
  226. struct dqstats dqstats;
  227. EXPORT_SYMBOL(dqstats);
  228. static qsize_t inode_get_rsv_space(struct inode *inode);
  229. static void __dquot_initialize(struct inode *inode, int type);
  230. static inline unsigned int
  231. hashfn(const struct super_block *sb, unsigned int id, int type)
  232. {
  233. unsigned long tmp;
  234. tmp = (((unsigned long)sb>>L1_CACHE_SHIFT) ^ id) * (MAXQUOTAS - type);
  235. return (tmp + (tmp >> dq_hash_bits)) & dq_hash_mask;
  236. }
  237. /*
  238. * Following list functions expect dq_list_lock to be held
  239. */
  240. static inline void insert_dquot_hash(struct dquot *dquot)
  241. {
  242. struct hlist_head *head;
  243. head = dquot_hash + hashfn(dquot->dq_sb, dquot->dq_id, dquot->dq_type);
  244. hlist_add_head(&dquot->dq_hash, head);
  245. }
  246. static inline void remove_dquot_hash(struct dquot *dquot)
  247. {
  248. hlist_del_init(&dquot->dq_hash);
  249. }
  250. static struct dquot *find_dquot(unsigned int hashent, struct super_block *sb,
  251. unsigned int id, int type)
  252. {
  253. struct hlist_node *node;
  254. struct dquot *dquot;
  255. hlist_for_each (node, dquot_hash+hashent) {
  256. dquot = hlist_entry(node, struct dquot, dq_hash);
  257. if (dquot->dq_sb == sb && dquot->dq_id == id &&
  258. dquot->dq_type == type)
  259. return dquot;
  260. }
  261. return NULL;
  262. }
  263. /* Add a dquot to the tail of the free list */
  264. static inline void put_dquot_last(struct dquot *dquot)
  265. {
  266. list_add_tail(&dquot->dq_free, &free_dquots);
  267. dqstats_inc(DQST_FREE_DQUOTS);
  268. }
  269. static inline void remove_free_dquot(struct dquot *dquot)
  270. {
  271. if (list_empty(&dquot->dq_free))
  272. return;
  273. list_del_init(&dquot->dq_free);
  274. dqstats_dec(DQST_FREE_DQUOTS);
  275. }
  276. static inline void put_inuse(struct dquot *dquot)
  277. {
  278. /* We add to the back of inuse list so we don't have to restart
  279. * when traversing this list and we block */
  280. list_add_tail(&dquot->dq_inuse, &inuse_list);
  281. dqstats_inc(DQST_ALLOC_DQUOTS);
  282. }
  283. static inline void remove_inuse(struct dquot *dquot)
  284. {
  285. dqstats_dec(DQST_ALLOC_DQUOTS);
  286. list_del(&dquot->dq_inuse);
  287. }
  288. /*
  289. * End of list functions needing dq_list_lock
  290. */
  291. static void wait_on_dquot(struct dquot *dquot)
  292. {
  293. mutex_lock(&dquot->dq_lock);
  294. mutex_unlock(&dquot->dq_lock);
  295. }
  296. static inline int dquot_dirty(struct dquot *dquot)
  297. {
  298. return test_bit(DQ_MOD_B, &dquot->dq_flags);
  299. }
  300. static inline int mark_dquot_dirty(struct dquot *dquot)
  301. {
  302. return dquot->dq_sb->dq_op->mark_dirty(dquot);
  303. }
  304. /* Mark dquot dirty in atomic manner, and return it's old dirty flag state */
  305. int dquot_mark_dquot_dirty(struct dquot *dquot)
  306. {
  307. int ret = 1;
  308. /* If quota is dirty already, we don't have to acquire dq_list_lock */
  309. if (test_bit(DQ_MOD_B, &dquot->dq_flags))
  310. return 1;
  311. spin_lock(&dq_list_lock);
  312. if (!test_and_set_bit(DQ_MOD_B, &dquot->dq_flags)) {
  313. list_add(&dquot->dq_dirty, &sb_dqopt(dquot->dq_sb)->
  314. info[dquot->dq_type].dqi_dirty_list);
  315. ret = 0;
  316. }
  317. spin_unlock(&dq_list_lock);
  318. return ret;
  319. }
  320. EXPORT_SYMBOL(dquot_mark_dquot_dirty);
  321. /* Dirtify all the dquots - this can block when journalling */
  322. static inline int mark_all_dquot_dirty(struct dquot * const *dquot)
  323. {
  324. int ret, err, cnt;
  325. ret = err = 0;
  326. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  327. if (dquot[cnt])
  328. /* Even in case of error we have to continue */
  329. ret = mark_dquot_dirty(dquot[cnt]);
  330. if (!err)
  331. err = ret;
  332. }
  333. return err;
  334. }
  335. static inline void dqput_all(struct dquot **dquot)
  336. {
  337. unsigned int cnt;
  338. for (cnt = 0; cnt < MAXQUOTAS; cnt++)
  339. dqput(dquot[cnt]);
  340. }
  341. /* This function needs dq_list_lock */
  342. static inline int clear_dquot_dirty(struct dquot *dquot)
  343. {
  344. if (!test_and_clear_bit(DQ_MOD_B, &dquot->dq_flags))
  345. return 0;
  346. list_del_init(&dquot->dq_dirty);
  347. return 1;
  348. }
  349. void mark_info_dirty(struct super_block *sb, int type)
  350. {
  351. set_bit(DQF_INFO_DIRTY_B, &sb_dqopt(sb)->info[type].dqi_flags);
  352. }
  353. EXPORT_SYMBOL(mark_info_dirty);
  354. /*
  355. * Read dquot from disk and alloc space for it
  356. */
  357. int dquot_acquire(struct dquot *dquot)
  358. {
  359. int ret = 0, ret2 = 0;
  360. struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
  361. mutex_lock(&dquot->dq_lock);
  362. mutex_lock(&dqopt->dqio_mutex);
  363. if (!test_bit(DQ_READ_B, &dquot->dq_flags))
  364. ret = dqopt->ops[dquot->dq_type]->read_dqblk(dquot);
  365. if (ret < 0)
  366. goto out_iolock;
  367. set_bit(DQ_READ_B, &dquot->dq_flags);
  368. /* Instantiate dquot if needed */
  369. if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags) && !dquot->dq_off) {
  370. ret = dqopt->ops[dquot->dq_type]->commit_dqblk(dquot);
  371. /* Write the info if needed */
  372. if (info_dirty(&dqopt->info[dquot->dq_type])) {
  373. ret2 = dqopt->ops[dquot->dq_type]->write_file_info(
  374. dquot->dq_sb, dquot->dq_type);
  375. }
  376. if (ret < 0)
  377. goto out_iolock;
  378. if (ret2 < 0) {
  379. ret = ret2;
  380. goto out_iolock;
  381. }
  382. }
  383. set_bit(DQ_ACTIVE_B, &dquot->dq_flags);
  384. out_iolock:
  385. mutex_unlock(&dqopt->dqio_mutex);
  386. mutex_unlock(&dquot->dq_lock);
  387. return ret;
  388. }
  389. EXPORT_SYMBOL(dquot_acquire);
  390. /*
  391. * Write dquot to disk
  392. */
  393. int dquot_commit(struct dquot *dquot)
  394. {
  395. int ret = 0;
  396. struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
  397. mutex_lock(&dqopt->dqio_mutex);
  398. spin_lock(&dq_list_lock);
  399. if (!clear_dquot_dirty(dquot)) {
  400. spin_unlock(&dq_list_lock);
  401. goto out_sem;
  402. }
  403. spin_unlock(&dq_list_lock);
  404. /* Inactive dquot can be only if there was error during read/init
  405. * => we have better not writing it */
  406. if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags))
  407. ret = dqopt->ops[dquot->dq_type]->commit_dqblk(dquot);
  408. else
  409. ret = -EIO;
  410. out_sem:
  411. mutex_unlock(&dqopt->dqio_mutex);
  412. return ret;
  413. }
  414. EXPORT_SYMBOL(dquot_commit);
  415. /*
  416. * Release dquot
  417. */
  418. int dquot_release(struct dquot *dquot)
  419. {
  420. int ret = 0, ret2 = 0;
  421. struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
  422. mutex_lock(&dquot->dq_lock);
  423. /* Check whether we are not racing with some other dqget() */
  424. if (atomic_read(&dquot->dq_count) > 1)
  425. goto out_dqlock;
  426. mutex_lock(&dqopt->dqio_mutex);
  427. if (dqopt->ops[dquot->dq_type]->release_dqblk) {
  428. ret = dqopt->ops[dquot->dq_type]->release_dqblk(dquot);
  429. /* Write the info */
  430. if (info_dirty(&dqopt->info[dquot->dq_type])) {
  431. ret2 = dqopt->ops[dquot->dq_type]->write_file_info(
  432. dquot->dq_sb, dquot->dq_type);
  433. }
  434. if (ret >= 0)
  435. ret = ret2;
  436. }
  437. clear_bit(DQ_ACTIVE_B, &dquot->dq_flags);
  438. mutex_unlock(&dqopt->dqio_mutex);
  439. out_dqlock:
  440. mutex_unlock(&dquot->dq_lock);
  441. return ret;
  442. }
  443. EXPORT_SYMBOL(dquot_release);
  444. void dquot_destroy(struct dquot *dquot)
  445. {
  446. kmem_cache_free(dquot_cachep, dquot);
  447. }
  448. EXPORT_SYMBOL(dquot_destroy);
  449. static inline void do_destroy_dquot(struct dquot *dquot)
  450. {
  451. dquot->dq_sb->dq_op->destroy_dquot(dquot);
  452. }
  453. /* Invalidate all dquots on the list. Note that this function is called after
  454. * quota is disabled and pointers from inodes removed so there cannot be new
  455. * quota users. There can still be some users of quotas due to inodes being
  456. * just deleted or pruned by prune_icache() (those are not attached to any
  457. * list) or parallel quotactl call. We have to wait for such users.
  458. */
  459. static void invalidate_dquots(struct super_block *sb, int type)
  460. {
  461. struct dquot *dquot, *tmp;
  462. restart:
  463. spin_lock(&dq_list_lock);
  464. list_for_each_entry_safe(dquot, tmp, &inuse_list, dq_inuse) {
  465. if (dquot->dq_sb != sb)
  466. continue;
  467. if (dquot->dq_type != type)
  468. continue;
  469. /* Wait for dquot users */
  470. if (atomic_read(&dquot->dq_count)) {
  471. DEFINE_WAIT(wait);
  472. atomic_inc(&dquot->dq_count);
  473. prepare_to_wait(&dquot->dq_wait_unused, &wait,
  474. TASK_UNINTERRUPTIBLE);
  475. spin_unlock(&dq_list_lock);
  476. /* Once dqput() wakes us up, we know it's time to free
  477. * the dquot.
  478. * IMPORTANT: we rely on the fact that there is always
  479. * at most one process waiting for dquot to free.
  480. * Otherwise dq_count would be > 1 and we would never
  481. * wake up.
  482. */
  483. if (atomic_read(&dquot->dq_count) > 1)
  484. schedule();
  485. finish_wait(&dquot->dq_wait_unused, &wait);
  486. dqput(dquot);
  487. /* At this moment dquot() need not exist (it could be
  488. * reclaimed by prune_dqcache(). Hence we must
  489. * restart. */
  490. goto restart;
  491. }
  492. /*
  493. * Quota now has no users and it has been written on last
  494. * dqput()
  495. */
  496. remove_dquot_hash(dquot);
  497. remove_free_dquot(dquot);
  498. remove_inuse(dquot);
  499. do_destroy_dquot(dquot);
  500. }
  501. spin_unlock(&dq_list_lock);
  502. }
  503. /* Call callback for every active dquot on given filesystem */
  504. int dquot_scan_active(struct super_block *sb,
  505. int (*fn)(struct dquot *dquot, unsigned long priv),
  506. unsigned long priv)
  507. {
  508. struct dquot *dquot, *old_dquot = NULL;
  509. int ret = 0;
  510. mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
  511. spin_lock(&dq_list_lock);
  512. list_for_each_entry(dquot, &inuse_list, dq_inuse) {
  513. if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags))
  514. continue;
  515. if (dquot->dq_sb != sb)
  516. continue;
  517. /* Now we have active dquot so we can just increase use count */
  518. atomic_inc(&dquot->dq_count);
  519. spin_unlock(&dq_list_lock);
  520. dqstats_inc(DQST_LOOKUPS);
  521. dqput(old_dquot);
  522. old_dquot = dquot;
  523. ret = fn(dquot, priv);
  524. if (ret < 0)
  525. goto out;
  526. spin_lock(&dq_list_lock);
  527. /* We are safe to continue now because our dquot could not
  528. * be moved out of the inuse list while we hold the reference */
  529. }
  530. spin_unlock(&dq_list_lock);
  531. out:
  532. dqput(old_dquot);
  533. mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
  534. return ret;
  535. }
  536. EXPORT_SYMBOL(dquot_scan_active);
  537. int dquot_quota_sync(struct super_block *sb, int type, int wait)
  538. {
  539. struct list_head *dirty;
  540. struct dquot *dquot;
  541. struct quota_info *dqopt = sb_dqopt(sb);
  542. int cnt;
  543. mutex_lock(&dqopt->dqonoff_mutex);
  544. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  545. if (type != -1 && cnt != type)
  546. continue;
  547. if (!sb_has_quota_active(sb, cnt))
  548. continue;
  549. spin_lock(&dq_list_lock);
  550. dirty = &dqopt->info[cnt].dqi_dirty_list;
  551. while (!list_empty(dirty)) {
  552. dquot = list_first_entry(dirty, struct dquot,
  553. dq_dirty);
  554. /* Dirty and inactive can be only bad dquot... */
  555. if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
  556. clear_dquot_dirty(dquot);
  557. continue;
  558. }
  559. /* Now we have active dquot from which someone is
  560. * holding reference so we can safely just increase
  561. * use count */
  562. atomic_inc(&dquot->dq_count);
  563. spin_unlock(&dq_list_lock);
  564. dqstats_inc(DQST_LOOKUPS);
  565. sb->dq_op->write_dquot(dquot);
  566. dqput(dquot);
  567. spin_lock(&dq_list_lock);
  568. }
  569. spin_unlock(&dq_list_lock);
  570. }
  571. for (cnt = 0; cnt < MAXQUOTAS; cnt++)
  572. if ((cnt == type || type == -1) && sb_has_quota_active(sb, cnt)
  573. && info_dirty(&dqopt->info[cnt]))
  574. sb->dq_op->write_info(sb, cnt);
  575. dqstats_inc(DQST_SYNCS);
  576. mutex_unlock(&dqopt->dqonoff_mutex);
  577. if (!wait || (sb_dqopt(sb)->flags & DQUOT_QUOTA_SYS_FILE))
  578. return 0;
  579. /* This is not very clever (and fast) but currently I don't know about
  580. * any other simple way of getting quota data to disk and we must get
  581. * them there for userspace to be visible... */
  582. if (sb->s_op->sync_fs)
  583. sb->s_op->sync_fs(sb, 1);
  584. sync_blockdev(sb->s_bdev);
  585. /*
  586. * Now when everything is written we can discard the pagecache so
  587. * that userspace sees the changes.
  588. */
  589. mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
  590. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  591. if (type != -1 && cnt != type)
  592. continue;
  593. if (!sb_has_quota_active(sb, cnt))
  594. continue;
  595. mutex_lock_nested(&sb_dqopt(sb)->files[cnt]->i_mutex,
  596. I_MUTEX_QUOTA);
  597. truncate_inode_pages(&sb_dqopt(sb)->files[cnt]->i_data, 0);
  598. mutex_unlock(&sb_dqopt(sb)->files[cnt]->i_mutex);
  599. }
  600. mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
  601. return 0;
  602. }
  603. EXPORT_SYMBOL(dquot_quota_sync);
  604. /* Free unused dquots from cache */
  605. static void prune_dqcache(int count)
  606. {
  607. struct list_head *head;
  608. struct dquot *dquot;
  609. head = free_dquots.prev;
  610. while (head != &free_dquots && count) {
  611. dquot = list_entry(head, struct dquot, dq_free);
  612. remove_dquot_hash(dquot);
  613. remove_free_dquot(dquot);
  614. remove_inuse(dquot);
  615. do_destroy_dquot(dquot);
  616. count--;
  617. head = free_dquots.prev;
  618. }
  619. }
  620. /*
  621. * This is called from kswapd when we think we need some
  622. * more memory
  623. */
  624. static int shrink_dqcache_memory(struct shrinker *shrink,
  625. struct shrink_control *sc)
  626. {
  627. int nr = sc->nr_to_scan;
  628. if (nr) {
  629. spin_lock(&dq_list_lock);
  630. prune_dqcache(nr);
  631. spin_unlock(&dq_list_lock);
  632. }
  633. return ((unsigned)
  634. percpu_counter_read_positive(&dqstats.counter[DQST_FREE_DQUOTS])
  635. /100) * sysctl_vfs_cache_pressure;
  636. }
  637. static struct shrinker dqcache_shrinker = {
  638. .shrink = shrink_dqcache_memory,
  639. .seeks = DEFAULT_SEEKS,
  640. };
  641. /*
  642. * Put reference to dquot
  643. * NOTE: If you change this function please check whether dqput_blocks() works right...
  644. */
  645. void dqput(struct dquot *dquot)
  646. {
  647. int ret;
  648. if (!dquot)
  649. return;
  650. #ifdef CONFIG_QUOTA_DEBUG
  651. if (!atomic_read(&dquot->dq_count)) {
  652. quota_error(dquot->dq_sb, "trying to free free dquot of %s %d",
  653. quotatypes[dquot->dq_type], dquot->dq_id);
  654. BUG();
  655. }
  656. #endif
  657. dqstats_inc(DQST_DROPS);
  658. we_slept:
  659. spin_lock(&dq_list_lock);
  660. if (atomic_read(&dquot->dq_count) > 1) {
  661. /* We have more than one user... nothing to do */
  662. atomic_dec(&dquot->dq_count);
  663. /* Releasing dquot during quotaoff phase? */
  664. if (!sb_has_quota_active(dquot->dq_sb, dquot->dq_type) &&
  665. atomic_read(&dquot->dq_count) == 1)
  666. wake_up(&dquot->dq_wait_unused);
  667. spin_unlock(&dq_list_lock);
  668. return;
  669. }
  670. /* Need to release dquot? */
  671. if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags) && dquot_dirty(dquot)) {
  672. spin_unlock(&dq_list_lock);
  673. /* Commit dquot before releasing */
  674. ret = dquot->dq_sb->dq_op->write_dquot(dquot);
  675. if (ret < 0) {
  676. quota_error(dquot->dq_sb, "Can't write quota structure"
  677. " (error %d). Quota may get out of sync!",
  678. ret);
  679. /*
  680. * We clear dirty bit anyway, so that we avoid
  681. * infinite loop here
  682. */
  683. spin_lock(&dq_list_lock);
  684. clear_dquot_dirty(dquot);
  685. spin_unlock(&dq_list_lock);
  686. }
  687. goto we_slept;
  688. }
  689. /* Clear flag in case dquot was inactive (something bad happened) */
  690. clear_dquot_dirty(dquot);
  691. if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
  692. spin_unlock(&dq_list_lock);
  693. dquot->dq_sb->dq_op->release_dquot(dquot);
  694. goto we_slept;
  695. }
  696. atomic_dec(&dquot->dq_count);
  697. #ifdef CONFIG_QUOTA_DEBUG
  698. /* sanity check */
  699. BUG_ON(!list_empty(&dquot->dq_free));
  700. #endif
  701. put_dquot_last(dquot);
  702. spin_unlock(&dq_list_lock);
  703. }
  704. EXPORT_SYMBOL(dqput);
  705. struct dquot *dquot_alloc(struct super_block *sb, int type)
  706. {
  707. return kmem_cache_zalloc(dquot_cachep, GFP_NOFS);
  708. }
  709. EXPORT_SYMBOL(dquot_alloc);
  710. static struct dquot *get_empty_dquot(struct super_block *sb, int type)
  711. {
  712. struct dquot *dquot;
  713. dquot = sb->dq_op->alloc_dquot(sb, type);
  714. if(!dquot)
  715. return NULL;
  716. mutex_init(&dquot->dq_lock);
  717. INIT_LIST_HEAD(&dquot->dq_free);
  718. INIT_LIST_HEAD(&dquot->dq_inuse);
  719. INIT_HLIST_NODE(&dquot->dq_hash);
  720. INIT_LIST_HEAD(&dquot->dq_dirty);
  721. init_waitqueue_head(&dquot->dq_wait_unused);
  722. dquot->dq_sb = sb;
  723. dquot->dq_type = type;
  724. atomic_set(&dquot->dq_count, 1);
  725. return dquot;
  726. }
  727. /*
  728. * Get reference to dquot
  729. *
  730. * Locking is slightly tricky here. We are guarded from parallel quotaoff()
  731. * destroying our dquot by:
  732. * a) checking for quota flags under dq_list_lock and
  733. * b) getting a reference to dquot before we release dq_list_lock
  734. */
  735. struct dquot *dqget(struct super_block *sb, unsigned int id, int type)
  736. {
  737. unsigned int hashent = hashfn(sb, id, type);
  738. struct dquot *dquot = NULL, *empty = NULL;
  739. if (!sb_has_quota_active(sb, type))
  740. return NULL;
  741. we_slept:
  742. spin_lock(&dq_list_lock);
  743. spin_lock(&dq_state_lock);
  744. if (!sb_has_quota_active(sb, type)) {
  745. spin_unlock(&dq_state_lock);
  746. spin_unlock(&dq_list_lock);
  747. goto out;
  748. }
  749. spin_unlock(&dq_state_lock);
  750. dquot = find_dquot(hashent, sb, id, type);
  751. if (!dquot) {
  752. if (!empty) {
  753. spin_unlock(&dq_list_lock);
  754. empty = get_empty_dquot(sb, type);
  755. if (!empty)
  756. schedule(); /* Try to wait for a moment... */
  757. goto we_slept;
  758. }
  759. dquot = empty;
  760. empty = NULL;
  761. dquot->dq_id = id;
  762. /* all dquots go on the inuse_list */
  763. put_inuse(dquot);
  764. /* hash it first so it can be found */
  765. insert_dquot_hash(dquot);
  766. spin_unlock(&dq_list_lock);
  767. dqstats_inc(DQST_LOOKUPS);
  768. } else {
  769. if (!atomic_read(&dquot->dq_count))
  770. remove_free_dquot(dquot);
  771. atomic_inc(&dquot->dq_count);
  772. spin_unlock(&dq_list_lock);
  773. dqstats_inc(DQST_CACHE_HITS);
  774. dqstats_inc(DQST_LOOKUPS);
  775. }
  776. /* Wait for dq_lock - after this we know that either dquot_release() is
  777. * already finished or it will be canceled due to dq_count > 1 test */
  778. wait_on_dquot(dquot);
  779. /* Read the dquot / allocate space in quota file */
  780. if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags) &&
  781. sb->dq_op->acquire_dquot(dquot) < 0) {
  782. dqput(dquot);
  783. dquot = NULL;
  784. goto out;
  785. }
  786. #ifdef CONFIG_QUOTA_DEBUG
  787. BUG_ON(!dquot->dq_sb); /* Has somebody invalidated entry under us? */
  788. #endif
  789. out:
  790. if (empty)
  791. do_destroy_dquot(empty);
  792. return dquot;
  793. }
  794. EXPORT_SYMBOL(dqget);
  795. static int dqinit_needed(struct inode *inode, int type)
  796. {
  797. int cnt;
  798. if (IS_NOQUOTA(inode))
  799. return 0;
  800. if (type != -1)
  801. return !inode->i_dquot[type];
  802. for (cnt = 0; cnt < MAXQUOTAS; cnt++)
  803. if (!inode->i_dquot[cnt])
  804. return 1;
  805. return 0;
  806. }
  807. /* This routine is guarded by dqonoff_mutex mutex */
  808. static void add_dquot_ref(struct super_block *sb, int type)
  809. {
  810. struct inode *inode, *old_inode = NULL;
  811. #ifdef CONFIG_QUOTA_DEBUG
  812. int reserved = 0;
  813. #endif
  814. spin_lock(&inode_sb_list_lock);
  815. list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
  816. spin_lock(&inode->i_lock);
  817. if ((inode->i_state & (I_FREEING|I_WILL_FREE|I_NEW)) ||
  818. !atomic_read(&inode->i_writecount) ||
  819. !dqinit_needed(inode, type)) {
  820. spin_unlock(&inode->i_lock);
  821. continue;
  822. }
  823. #ifdef CONFIG_QUOTA_DEBUG
  824. if (unlikely(inode_get_rsv_space(inode) > 0))
  825. reserved = 1;
  826. #endif
  827. __iget(inode);
  828. spin_unlock(&inode->i_lock);
  829. spin_unlock(&inode_sb_list_lock);
  830. iput(old_inode);
  831. __dquot_initialize(inode, type);
  832. /*
  833. * We hold a reference to 'inode' so it couldn't have been
  834. * removed from s_inodes list while we dropped the
  835. * inode_sb_list_lock We cannot iput the inode now as we can be
  836. * holding the last reference and we cannot iput it under
  837. * inode_sb_list_lock. So we keep the reference and iput it
  838. * later.
  839. */
  840. old_inode = inode;
  841. spin_lock(&inode_sb_list_lock);
  842. }
  843. spin_unlock(&inode_sb_list_lock);
  844. iput(old_inode);
  845. #ifdef CONFIG_QUOTA_DEBUG
  846. if (reserved) {
  847. quota_error(sb, "Writes happened before quota was turned on "
  848. "thus quota information is probably inconsistent. "
  849. "Please run quotacheck(8)");
  850. }
  851. #endif
  852. }
  853. /*
  854. * Return 0 if dqput() won't block.
  855. * (note that 1 doesn't necessarily mean blocking)
  856. */
  857. static inline int dqput_blocks(struct dquot *dquot)
  858. {
  859. if (atomic_read(&dquot->dq_count) <= 1)
  860. return 1;
  861. return 0;
  862. }
  863. /*
  864. * Remove references to dquots from inode and add dquot to list for freeing
  865. * if we have the last reference to dquot
  866. * We can't race with anybody because we hold dqptr_sem for writing...
  867. */
  868. static int remove_inode_dquot_ref(struct inode *inode, int type,
  869. struct list_head *tofree_head)
  870. {
  871. struct dquot *dquot = inode->i_dquot[type];
  872. inode->i_dquot[type] = NULL;
  873. if (dquot) {
  874. if (dqput_blocks(dquot)) {
  875. #ifdef CONFIG_QUOTA_DEBUG
  876. if (atomic_read(&dquot->dq_count) != 1)
  877. quota_error(inode->i_sb, "Adding dquot with "
  878. "dq_count %d to dispose list",
  879. atomic_read(&dquot->dq_count));
  880. #endif
  881. spin_lock(&dq_list_lock);
  882. /* As dquot must have currently users it can't be on
  883. * the free list... */
  884. list_add(&dquot->dq_free, tofree_head);
  885. spin_unlock(&dq_list_lock);
  886. return 1;
  887. }
  888. else
  889. dqput(dquot); /* We have guaranteed we won't block */
  890. }
  891. return 0;
  892. }
  893. /*
  894. * Free list of dquots
  895. * Dquots are removed from inodes and no new references can be got so we are
  896. * the only ones holding reference
  897. */
  898. static void put_dquot_list(struct list_head *tofree_head)
  899. {
  900. struct list_head *act_head;
  901. struct dquot *dquot;
  902. act_head = tofree_head->next;
  903. while (act_head != tofree_head) {
  904. dquot = list_entry(act_head, struct dquot, dq_free);
  905. act_head = act_head->next;
  906. /* Remove dquot from the list so we won't have problems... */
  907. list_del_init(&dquot->dq_free);
  908. dqput(dquot);
  909. }
  910. }
  911. static void remove_dquot_ref(struct super_block *sb, int type,
  912. struct list_head *tofree_head)
  913. {
  914. struct inode *inode;
  915. int reserved = 0;
  916. spin_lock(&inode_sb_list_lock);
  917. list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
  918. /*
  919. * We have to scan also I_NEW inodes because they can already
  920. * have quota pointer initialized. Luckily, we need to touch
  921. * only quota pointers and these have separate locking
  922. * (dqptr_sem).
  923. */
  924. if (!IS_NOQUOTA(inode)) {
  925. if (unlikely(inode_get_rsv_space(inode) > 0))
  926. reserved = 1;
  927. remove_inode_dquot_ref(inode, type, tofree_head);
  928. }
  929. }
  930. spin_unlock(&inode_sb_list_lock);
  931. #ifdef CONFIG_QUOTA_DEBUG
  932. if (reserved) {
  933. printk(KERN_WARNING "VFS (%s): Writes happened after quota"
  934. " was disabled thus quota information is probably "
  935. "inconsistent. Please run quotacheck(8).\n", sb->s_id);
  936. }
  937. #endif
  938. }
  939. /* Gather all references from inodes and drop them */
  940. static void drop_dquot_ref(struct super_block *sb, int type)
  941. {
  942. LIST_HEAD(tofree_head);
  943. if (sb->dq_op) {
  944. down_write(&sb_dqopt(sb)->dqptr_sem);
  945. remove_dquot_ref(sb, type, &tofree_head);
  946. up_write(&sb_dqopt(sb)->dqptr_sem);
  947. put_dquot_list(&tofree_head);
  948. }
  949. }
  950. static inline void dquot_incr_inodes(struct dquot *dquot, qsize_t number)
  951. {
  952. dquot->dq_dqb.dqb_curinodes += number;
  953. }
  954. static inline void dquot_incr_space(struct dquot *dquot, qsize_t number)
  955. {
  956. dquot->dq_dqb.dqb_curspace += number;
  957. }
  958. static inline void dquot_resv_space(struct dquot *dquot, qsize_t number)
  959. {
  960. dquot->dq_dqb.dqb_rsvspace += number;
  961. }
  962. /*
  963. * Claim reserved quota space
  964. */
  965. static void dquot_claim_reserved_space(struct dquot *dquot, qsize_t number)
  966. {
  967. if (dquot->dq_dqb.dqb_rsvspace < number) {
  968. WARN_ON_ONCE(1);
  969. number = dquot->dq_dqb.dqb_rsvspace;
  970. }
  971. dquot->dq_dqb.dqb_curspace += number;
  972. dquot->dq_dqb.dqb_rsvspace -= number;
  973. }
  974. static inline
  975. void dquot_free_reserved_space(struct dquot *dquot, qsize_t number)
  976. {
  977. if (dquot->dq_dqb.dqb_rsvspace >= number)
  978. dquot->dq_dqb.dqb_rsvspace -= number;
  979. else {
  980. WARN_ON_ONCE(1);
  981. dquot->dq_dqb.dqb_rsvspace = 0;
  982. }
  983. }
  984. static void dquot_decr_inodes(struct dquot *dquot, qsize_t number)
  985. {
  986. if (sb_dqopt(dquot->dq_sb)->flags & DQUOT_NEGATIVE_USAGE ||
  987. dquot->dq_dqb.dqb_curinodes >= number)
  988. dquot->dq_dqb.dqb_curinodes -= number;
  989. else
  990. dquot->dq_dqb.dqb_curinodes = 0;
  991. if (dquot->dq_dqb.dqb_curinodes <= dquot->dq_dqb.dqb_isoftlimit)
  992. dquot->dq_dqb.dqb_itime = (time_t) 0;
  993. clear_bit(DQ_INODES_B, &dquot->dq_flags);
  994. }
  995. static void dquot_decr_space(struct dquot *dquot, qsize_t number)
  996. {
  997. if (sb_dqopt(dquot->dq_sb)->flags & DQUOT_NEGATIVE_USAGE ||
  998. dquot->dq_dqb.dqb_curspace >= number)
  999. dquot->dq_dqb.dqb_curspace -= number;
  1000. else
  1001. dquot->dq_dqb.dqb_curspace = 0;
  1002. if (dquot->dq_dqb.dqb_curspace <= dquot->dq_dqb.dqb_bsoftlimit)
  1003. dquot->dq_dqb.dqb_btime = (time_t) 0;
  1004. clear_bit(DQ_BLKS_B, &dquot->dq_flags);
  1005. }
  1006. static int warning_issued(struct dquot *dquot, const int warntype)
  1007. {
  1008. int flag = (warntype == QUOTA_NL_BHARDWARN ||
  1009. warntype == QUOTA_NL_BSOFTLONGWARN) ? DQ_BLKS_B :
  1010. ((warntype == QUOTA_NL_IHARDWARN ||
  1011. warntype == QUOTA_NL_ISOFTLONGWARN) ? DQ_INODES_B : 0);
  1012. if (!flag)
  1013. return 0;
  1014. return test_and_set_bit(flag, &dquot->dq_flags);
  1015. }
  1016. #ifdef CONFIG_PRINT_QUOTA_WARNING
  1017. static int flag_print_warnings = 1;
  1018. static int need_print_warning(struct dquot *dquot)
  1019. {
  1020. if (!flag_print_warnings)
  1021. return 0;
  1022. switch (dquot->dq_type) {
  1023. case USRQUOTA:
  1024. return current_fsuid() == dquot->dq_id;
  1025. case GRPQUOTA:
  1026. return in_group_p(dquot->dq_id);
  1027. }
  1028. return 0;
  1029. }
  1030. /* Print warning to user which exceeded quota */
  1031. static void print_warning(struct dquot *dquot, const int warntype)
  1032. {
  1033. char *msg = NULL;
  1034. struct tty_struct *tty;
  1035. if (warntype == QUOTA_NL_IHARDBELOW ||
  1036. warntype == QUOTA_NL_ISOFTBELOW ||
  1037. warntype == QUOTA_NL_BHARDBELOW ||
  1038. warntype == QUOTA_NL_BSOFTBELOW || !need_print_warning(dquot))
  1039. return;
  1040. tty = get_current_tty();
  1041. if (!tty)
  1042. return;
  1043. tty_write_message(tty, dquot->dq_sb->s_id);
  1044. if (warntype == QUOTA_NL_ISOFTWARN || warntype == QUOTA_NL_BSOFTWARN)
  1045. tty_write_message(tty, ": warning, ");
  1046. else
  1047. tty_write_message(tty, ": write failed, ");
  1048. tty_write_message(tty, quotatypes[dquot->dq_type]);
  1049. switch (warntype) {
  1050. case QUOTA_NL_IHARDWARN:
  1051. msg = " file limit reached.\r\n";
  1052. break;
  1053. case QUOTA_NL_ISOFTLONGWARN:
  1054. msg = " file quota exceeded too long.\r\n";
  1055. break;
  1056. case QUOTA_NL_ISOFTWARN:
  1057. msg = " file quota exceeded.\r\n";
  1058. break;
  1059. case QUOTA_NL_BHARDWARN:
  1060. msg = " block limit reached.\r\n";
  1061. break;
  1062. case QUOTA_NL_BSOFTLONGWARN:
  1063. msg = " block quota exceeded too long.\r\n";
  1064. break;
  1065. case QUOTA_NL_BSOFTWARN:
  1066. msg = " block quota exceeded.\r\n";
  1067. break;
  1068. }
  1069. tty_write_message(tty, msg);
  1070. tty_kref_put(tty);
  1071. }
  1072. #endif
  1073. /*
  1074. * Write warnings to the console and send warning messages over netlink.
  1075. *
  1076. * Note that this function can sleep.
  1077. */
  1078. static void flush_warnings(struct dquot *const *dquots, char *warntype)
  1079. {
  1080. struct dquot *dq;
  1081. int i;
  1082. for (i = 0; i < MAXQUOTAS; i++) {
  1083. dq = dquots[i];
  1084. if (dq && warntype[i] != QUOTA_NL_NOWARN &&
  1085. !warning_issued(dq, warntype[i])) {
  1086. #ifdef CONFIG_PRINT_QUOTA_WARNING
  1087. print_warning(dq, warntype[i]);
  1088. #endif
  1089. quota_send_warning(dq->dq_type, dq->dq_id,
  1090. dq->dq_sb->s_dev, warntype[i]);
  1091. }
  1092. }
  1093. }
  1094. static int ignore_hardlimit(struct dquot *dquot)
  1095. {
  1096. struct mem_dqinfo *info = &sb_dqopt(dquot->dq_sb)->info[dquot->dq_type];
  1097. return capable(CAP_SYS_RESOURCE) &&
  1098. (info->dqi_format->qf_fmt_id != QFMT_VFS_OLD ||
  1099. !(info->dqi_flags & V1_DQF_RSQUASH));
  1100. }
  1101. /* needs dq_data_lock */
  1102. static int check_idq(struct dquot *dquot, qsize_t inodes, char *warntype)
  1103. {
  1104. qsize_t newinodes = dquot->dq_dqb.dqb_curinodes + inodes;
  1105. *warntype = QUOTA_NL_NOWARN;
  1106. if (!sb_has_quota_limits_enabled(dquot->dq_sb, dquot->dq_type) ||
  1107. test_bit(DQ_FAKE_B, &dquot->dq_flags))
  1108. return 0;
  1109. if (dquot->dq_dqb.dqb_ihardlimit &&
  1110. newinodes > dquot->dq_dqb.dqb_ihardlimit &&
  1111. !ignore_hardlimit(dquot)) {
  1112. *warntype = QUOTA_NL_IHARDWARN;
  1113. return -EDQUOT;
  1114. }
  1115. if (dquot->dq_dqb.dqb_isoftlimit &&
  1116. newinodes > dquot->dq_dqb.dqb_isoftlimit &&
  1117. dquot->dq_dqb.dqb_itime &&
  1118. get_seconds() >= dquot->dq_dqb.dqb_itime &&
  1119. !ignore_hardlimit(dquot)) {
  1120. *warntype = QUOTA_NL_ISOFTLONGWARN;
  1121. return -EDQUOT;
  1122. }
  1123. if (dquot->dq_dqb.dqb_isoftlimit &&
  1124. newinodes > dquot->dq_dqb.dqb_isoftlimit &&
  1125. dquot->dq_dqb.dqb_itime == 0) {
  1126. *warntype = QUOTA_NL_ISOFTWARN;
  1127. dquot->dq_dqb.dqb_itime = get_seconds() +
  1128. sb_dqopt(dquot->dq_sb)->info[dquot->dq_type].dqi_igrace;
  1129. }
  1130. return 0;
  1131. }
  1132. /* needs dq_data_lock */
  1133. static int check_bdq(struct dquot *dquot, qsize_t space, int prealloc, char *warntype)
  1134. {
  1135. qsize_t tspace;
  1136. struct super_block *sb = dquot->dq_sb;
  1137. *warntype = QUOTA_NL_NOWARN;
  1138. if (!sb_has_quota_limits_enabled(sb, dquot->dq_type) ||
  1139. test_bit(DQ_FAKE_B, &dquot->dq_flags))
  1140. return 0;
  1141. tspace = dquot->dq_dqb.dqb_curspace + dquot->dq_dqb.dqb_rsvspace
  1142. + space;
  1143. if (dquot->dq_dqb.dqb_bhardlimit &&
  1144. tspace > dquot->dq_dqb.dqb_bhardlimit &&
  1145. !ignore_hardlimit(dquot)) {
  1146. if (!prealloc)
  1147. *warntype = QUOTA_NL_BHARDWARN;
  1148. return -EDQUOT;
  1149. }
  1150. if (dquot->dq_dqb.dqb_bsoftlimit &&
  1151. tspace > dquot->dq_dqb.dqb_bsoftlimit &&
  1152. dquot->dq_dqb.dqb_btime &&
  1153. get_seconds() >= dquot->dq_dqb.dqb_btime &&
  1154. !ignore_hardlimit(dquot)) {
  1155. if (!prealloc)
  1156. *warntype = QUOTA_NL_BSOFTLONGWARN;
  1157. return -EDQUOT;
  1158. }
  1159. if (dquot->dq_dqb.dqb_bsoftlimit &&
  1160. tspace > dquot->dq_dqb.dqb_bsoftlimit &&
  1161. dquot->dq_dqb.dqb_btime == 0) {
  1162. if (!prealloc) {
  1163. *warntype = QUOTA_NL_BSOFTWARN;
  1164. dquot->dq_dqb.dqb_btime = get_seconds() +
  1165. sb_dqopt(sb)->info[dquot->dq_type].dqi_bgrace;
  1166. }
  1167. else
  1168. /*
  1169. * We don't allow preallocation to exceed softlimit so exceeding will
  1170. * be always printed
  1171. */
  1172. return -EDQUOT;
  1173. }
  1174. return 0;
  1175. }
  1176. static int info_idq_free(struct dquot *dquot, qsize_t inodes)
  1177. {
  1178. qsize_t newinodes;
  1179. if (test_bit(DQ_FAKE_B, &dquot->dq_flags) ||
  1180. dquot->dq_dqb.dqb_curinodes <= dquot->dq_dqb.dqb_isoftlimit ||
  1181. !sb_has_quota_limits_enabled(dquot->dq_sb, dquot->dq_type))
  1182. return QUOTA_NL_NOWARN;
  1183. newinodes = dquot->dq_dqb.dqb_curinodes - inodes;
  1184. if (newinodes <= dquot->dq_dqb.dqb_isoftlimit)
  1185. return QUOTA_NL_ISOFTBELOW;
  1186. if (dquot->dq_dqb.dqb_curinodes >= dquot->dq_dqb.dqb_ihardlimit &&
  1187. newinodes < dquot->dq_dqb.dqb_ihardlimit)
  1188. return QUOTA_NL_IHARDBELOW;
  1189. return QUOTA_NL_NOWARN;
  1190. }
  1191. static int info_bdq_free(struct dquot *dquot, qsize_t space)
  1192. {
  1193. if (test_bit(DQ_FAKE_B, &dquot->dq_flags) ||
  1194. dquot->dq_dqb.dqb_curspace <= dquot->dq_dqb.dqb_bsoftlimit)
  1195. return QUOTA_NL_NOWARN;
  1196. if (dquot->dq_dqb.dqb_curspace - space <= dquot->dq_dqb.dqb_bsoftlimit)
  1197. return QUOTA_NL_BSOFTBELOW;
  1198. if (dquot->dq_dqb.dqb_curspace >= dquot->dq_dqb.dqb_bhardlimit &&
  1199. dquot->dq_dqb.dqb_curspace - space < dquot->dq_dqb.dqb_bhardlimit)
  1200. return QUOTA_NL_BHARDBELOW;
  1201. return QUOTA_NL_NOWARN;
  1202. }
  1203. static int dquot_active(const struct inode *inode)
  1204. {
  1205. struct super_block *sb = inode->i_sb;
  1206. if (IS_NOQUOTA(inode))
  1207. return 0;
  1208. return sb_any_quota_loaded(sb) & ~sb_any_quota_suspended(sb);
  1209. }
  1210. /*
  1211. * Initialize quota pointers in inode
  1212. *
  1213. * We do things in a bit complicated way but by that we avoid calling
  1214. * dqget() and thus filesystem callbacks under dqptr_sem.
  1215. *
  1216. * It is better to call this function outside of any transaction as it
  1217. * might need a lot of space in journal for dquot structure allocation.
  1218. */
  1219. static void __dquot_initialize(struct inode *inode, int type)
  1220. {
  1221. unsigned int id = 0;
  1222. int cnt;
  1223. struct dquot *got[MAXQUOTAS];
  1224. struct super_block *sb = inode->i_sb;
  1225. qsize_t rsv;
  1226. /* First test before acquiring mutex - solves deadlocks when we
  1227. * re-enter the quota code and are already holding the mutex */
  1228. if (!dquot_active(inode))
  1229. return;
  1230. /* First get references to structures we might need. */
  1231. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1232. got[cnt] = NULL;
  1233. if (type != -1 && cnt != type)
  1234. continue;
  1235. switch (cnt) {
  1236. case USRQUOTA:
  1237. id = inode->i_uid;
  1238. break;
  1239. case GRPQUOTA:
  1240. id = inode->i_gid;
  1241. break;
  1242. }
  1243. got[cnt] = dqget(sb, id, cnt);
  1244. }
  1245. down_write(&sb_dqopt(sb)->dqptr_sem);
  1246. if (IS_NOQUOTA(inode))
  1247. goto out_err;
  1248. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1249. if (type != -1 && cnt != type)
  1250. continue;
  1251. /* Avoid races with quotaoff() */
  1252. if (!sb_has_quota_active(sb, cnt))
  1253. continue;
  1254. /* We could race with quotaon or dqget() could have failed */
  1255. if (!got[cnt])
  1256. continue;
  1257. if (!inode->i_dquot[cnt]) {
  1258. inode->i_dquot[cnt] = got[cnt];
  1259. got[cnt] = NULL;
  1260. /*
  1261. * Make quota reservation system happy if someone
  1262. * did a write before quota was turned on
  1263. */
  1264. rsv = inode_get_rsv_space(inode);
  1265. if (unlikely(rsv))
  1266. dquot_resv_space(inode->i_dquot[cnt], rsv);
  1267. }
  1268. }
  1269. out_err:
  1270. up_write(&sb_dqopt(sb)->dqptr_sem);
  1271. /* Drop unused references */
  1272. dqput_all(got);
  1273. }
  1274. void dquot_initialize(struct inode *inode)
  1275. {
  1276. __dquot_initialize(inode, -1);
  1277. }
  1278. EXPORT_SYMBOL(dquot_initialize);
  1279. /*
  1280. * Release all quotas referenced by inode
  1281. */
  1282. static void __dquot_drop(struct inode *inode)
  1283. {
  1284. int cnt;
  1285. struct dquot *put[MAXQUOTAS];
  1286. down_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1287. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1288. put[cnt] = inode->i_dquot[cnt];
  1289. inode->i_dquot[cnt] = NULL;
  1290. }
  1291. up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1292. dqput_all(put);
  1293. }
  1294. void dquot_drop(struct inode *inode)
  1295. {
  1296. int cnt;
  1297. if (IS_NOQUOTA(inode))
  1298. return;
  1299. /*
  1300. * Test before calling to rule out calls from proc and such
  1301. * where we are not allowed to block. Note that this is
  1302. * actually reliable test even without the lock - the caller
  1303. * must assure that nobody can come after the DQUOT_DROP and
  1304. * add quota pointers back anyway.
  1305. */
  1306. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1307. if (inode->i_dquot[cnt])
  1308. break;
  1309. }
  1310. if (cnt < MAXQUOTAS)
  1311. __dquot_drop(inode);
  1312. }
  1313. EXPORT_SYMBOL(dquot_drop);
  1314. /*
  1315. * inode_reserved_space is managed internally by quota, and protected by
  1316. * i_lock similar to i_blocks+i_bytes.
  1317. */
  1318. static qsize_t *inode_reserved_space(struct inode * inode)
  1319. {
  1320. /* Filesystem must explicitly define it's own method in order to use
  1321. * quota reservation interface */
  1322. BUG_ON(!inode->i_sb->dq_op->get_reserved_space);
  1323. return inode->i_sb->dq_op->get_reserved_space(inode);
  1324. }
  1325. void inode_add_rsv_space(struct inode *inode, qsize_t number)
  1326. {
  1327. spin_lock(&inode->i_lock);
  1328. *inode_reserved_space(inode) += number;
  1329. spin_unlock(&inode->i_lock);
  1330. }
  1331. EXPORT_SYMBOL(inode_add_rsv_space);
  1332. void inode_claim_rsv_space(struct inode *inode, qsize_t number)
  1333. {
  1334. spin_lock(&inode->i_lock);
  1335. *inode_reserved_space(inode) -= number;
  1336. __inode_add_bytes(inode, number);
  1337. spin_unlock(&inode->i_lock);
  1338. }
  1339. EXPORT_SYMBOL(inode_claim_rsv_space);
  1340. void inode_sub_rsv_space(struct inode *inode, qsize_t number)
  1341. {
  1342. spin_lock(&inode->i_lock);
  1343. *inode_reserved_space(inode) -= number;
  1344. spin_unlock(&inode->i_lock);
  1345. }
  1346. EXPORT_SYMBOL(inode_sub_rsv_space);
  1347. static qsize_t inode_get_rsv_space(struct inode *inode)
  1348. {
  1349. qsize_t ret;
  1350. if (!inode->i_sb->dq_op->get_reserved_space)
  1351. return 0;
  1352. spin_lock(&inode->i_lock);
  1353. ret = *inode_reserved_space(inode);
  1354. spin_unlock(&inode->i_lock);
  1355. return ret;
  1356. }
  1357. static void inode_incr_space(struct inode *inode, qsize_t number,
  1358. int reserve)
  1359. {
  1360. if (reserve)
  1361. inode_add_rsv_space(inode, number);
  1362. else
  1363. inode_add_bytes(inode, number);
  1364. }
  1365. static void inode_decr_space(struct inode *inode, qsize_t number, int reserve)
  1366. {
  1367. if (reserve)
  1368. inode_sub_rsv_space(inode, number);
  1369. else
  1370. inode_sub_bytes(inode, number);
  1371. }
  1372. /*
  1373. * This functions updates i_blocks+i_bytes fields and quota information
  1374. * (together with appropriate checks).
  1375. *
  1376. * NOTE: We absolutely rely on the fact that caller dirties the inode
  1377. * (usually helpers in quotaops.h care about this) and holds a handle for
  1378. * the current transaction so that dquot write and inode write go into the
  1379. * same transaction.
  1380. */
  1381. /*
  1382. * This operation can block, but only after everything is updated
  1383. */
  1384. int __dquot_alloc_space(struct inode *inode, qsize_t number, int flags)
  1385. {
  1386. int cnt, ret = 0;
  1387. char warntype[MAXQUOTAS];
  1388. int warn = flags & DQUOT_SPACE_WARN;
  1389. int reserve = flags & DQUOT_SPACE_RESERVE;
  1390. int nofail = flags & DQUOT_SPACE_NOFAIL;
  1391. /*
  1392. * First test before acquiring mutex - solves deadlocks when we
  1393. * re-enter the quota code and are already holding the mutex
  1394. */
  1395. if (!dquot_active(inode)) {
  1396. inode_incr_space(inode, number, reserve);
  1397. goto out;
  1398. }
  1399. down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1400. for (cnt = 0; cnt < MAXQUOTAS; cnt++)
  1401. warntype[cnt] = QUOTA_NL_NOWARN;
  1402. spin_lock(&dq_data_lock);
  1403. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1404. if (!inode->i_dquot[cnt])
  1405. continue;
  1406. ret = check_bdq(inode->i_dquot[cnt], number, !warn,
  1407. warntype+cnt);
  1408. if (ret && !nofail) {
  1409. spin_unlock(&dq_data_lock);
  1410. goto out_flush_warn;
  1411. }
  1412. }
  1413. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1414. if (!inode->i_dquot[cnt])
  1415. continue;
  1416. if (reserve)
  1417. dquot_resv_space(inode->i_dquot[cnt], number);
  1418. else
  1419. dquot_incr_space(inode->i_dquot[cnt], number);
  1420. }
  1421. inode_incr_space(inode, number, reserve);
  1422. spin_unlock(&dq_data_lock);
  1423. if (reserve)
  1424. goto out_flush_warn;
  1425. mark_all_dquot_dirty(inode->i_dquot);
  1426. out_flush_warn:
  1427. flush_warnings(inode->i_dquot, warntype);
  1428. up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1429. out:
  1430. return ret;
  1431. }
  1432. EXPORT_SYMBOL(__dquot_alloc_space);
  1433. /*
  1434. * This operation can block, but only after everything is updated
  1435. */
  1436. int dquot_alloc_inode(const struct inode *inode)
  1437. {
  1438. int cnt, ret = 0;
  1439. char warntype[MAXQUOTAS];
  1440. /* First test before acquiring mutex - solves deadlocks when we
  1441. * re-enter the quota code and are already holding the mutex */
  1442. if (!dquot_active(inode))
  1443. return 0;
  1444. for (cnt = 0; cnt < MAXQUOTAS; cnt++)
  1445. warntype[cnt] = QUOTA_NL_NOWARN;
  1446. down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1447. spin_lock(&dq_data_lock);
  1448. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1449. if (!inode->i_dquot[cnt])
  1450. continue;
  1451. ret = check_idq(inode->i_dquot[cnt], 1, warntype + cnt);
  1452. if (ret)
  1453. goto warn_put_all;
  1454. }
  1455. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1456. if (!inode->i_dquot[cnt])
  1457. continue;
  1458. dquot_incr_inodes(inode->i_dquot[cnt], 1);
  1459. }
  1460. warn_put_all:
  1461. spin_unlock(&dq_data_lock);
  1462. if (ret == 0)
  1463. mark_all_dquot_dirty(inode->i_dquot);
  1464. flush_warnings(inode->i_dquot, warntype);
  1465. up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1466. return ret;
  1467. }
  1468. EXPORT_SYMBOL(dquot_alloc_inode);
  1469. /*
  1470. * Convert in-memory reserved quotas to real consumed quotas
  1471. */
  1472. int dquot_claim_space_nodirty(struct inode *inode, qsize_t number)
  1473. {
  1474. int cnt;
  1475. if (!dquot_active(inode)) {
  1476. inode_claim_rsv_space(inode, number);
  1477. return 0;
  1478. }
  1479. down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1480. spin_lock(&dq_data_lock);
  1481. /* Claim reserved quotas to allocated quotas */
  1482. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1483. if (inode->i_dquot[cnt])
  1484. dquot_claim_reserved_space(inode->i_dquot[cnt],
  1485. number);
  1486. }
  1487. /* Update inode bytes */
  1488. inode_claim_rsv_space(inode, number);
  1489. spin_unlock(&dq_data_lock);
  1490. mark_all_dquot_dirty(inode->i_dquot);
  1491. up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1492. return 0;
  1493. }
  1494. EXPORT_SYMBOL(dquot_claim_space_nodirty);
  1495. /*
  1496. * This operation can block, but only after everything is updated
  1497. */
  1498. void __dquot_free_space(struct inode *inode, qsize_t number, int flags)
  1499. {
  1500. unsigned int cnt;
  1501. char warntype[MAXQUOTAS];
  1502. int reserve = flags & DQUOT_SPACE_RESERVE;
  1503. /* First test before acquiring mutex - solves deadlocks when we
  1504. * re-enter the quota code and are already holding the mutex */
  1505. if (!dquot_active(inode)) {
  1506. inode_decr_space(inode, number, reserve);
  1507. return;
  1508. }
  1509. down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1510. spin_lock(&dq_data_lock);
  1511. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1512. if (!inode->i_dquot[cnt])
  1513. continue;
  1514. warntype[cnt] = info_bdq_free(inode->i_dquot[cnt], number);
  1515. if (reserve)
  1516. dquot_free_reserved_space(inode->i_dquot[cnt], number);
  1517. else
  1518. dquot_decr_space(inode->i_dquot[cnt], number);
  1519. }
  1520. inode_decr_space(inode, number, reserve);
  1521. spin_unlock(&dq_data_lock);
  1522. if (reserve)
  1523. goto out_unlock;
  1524. mark_all_dquot_dirty(inode->i_dquot);
  1525. out_unlock:
  1526. flush_warnings(inode->i_dquot, warntype);
  1527. up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1528. }
  1529. EXPORT_SYMBOL(__dquot_free_space);
  1530. /*
  1531. * This operation can block, but only after everything is updated
  1532. */
  1533. void dquot_free_inode(const struct inode *inode)
  1534. {
  1535. unsigned int cnt;
  1536. char warntype[MAXQUOTAS];
  1537. /* First test before acquiring mutex - solves deadlocks when we
  1538. * re-enter the quota code and are already holding the mutex */
  1539. if (!dquot_active(inode))
  1540. return;
  1541. down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1542. spin_lock(&dq_data_lock);
  1543. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1544. if (!inode->i_dquot[cnt])
  1545. continue;
  1546. warntype[cnt] = info_idq_free(inode->i_dquot[cnt], 1);
  1547. dquot_decr_inodes(inode->i_dquot[cnt], 1);
  1548. }
  1549. spin_unlock(&dq_data_lock);
  1550. mark_all_dquot_dirty(inode->i_dquot);
  1551. flush_warnings(inode->i_dquot, warntype);
  1552. up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1553. }
  1554. EXPORT_SYMBOL(dquot_free_inode);
  1555. /*
  1556. * Transfer the number of inode and blocks from one diskquota to an other.
  1557. * On success, dquot references in transfer_to are consumed and references
  1558. * to original dquots that need to be released are placed there. On failure,
  1559. * references are kept untouched.
  1560. *
  1561. * This operation can block, but only after everything is updated
  1562. * A transaction must be started when entering this function.
  1563. *
  1564. */
  1565. int __dquot_transfer(struct inode *inode, struct dquot **transfer_to)
  1566. {
  1567. qsize_t space, cur_space;
  1568. qsize_t rsv_space = 0;
  1569. struct dquot *transfer_from[MAXQUOTAS] = {};
  1570. int cnt, ret = 0;
  1571. char is_valid[MAXQUOTAS] = {};
  1572. char warntype_to[MAXQUOTAS];
  1573. char warntype_from_inodes[MAXQUOTAS], warntype_from_space[MAXQUOTAS];
  1574. /* First test before acquiring mutex - solves deadlocks when we
  1575. * re-enter the quota code and are already holding the mutex */
  1576. if (IS_NOQUOTA(inode))
  1577. return 0;
  1578. /* Initialize the arrays */
  1579. for (cnt = 0; cnt < MAXQUOTAS; cnt++)
  1580. warntype_to[cnt] = QUOTA_NL_NOWARN;
  1581. down_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1582. if (IS_NOQUOTA(inode)) { /* File without quota accounting? */
  1583. up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1584. return 0;
  1585. }
  1586. spin_lock(&dq_data_lock);
  1587. cur_space = inode_get_bytes(inode);
  1588. rsv_space = inode_get_rsv_space(inode);
  1589. space = cur_space + rsv_space;
  1590. /* Build the transfer_from list and check the limits */
  1591. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1592. /*
  1593. * Skip changes for same uid or gid or for turned off quota-type.
  1594. */
  1595. if (!transfer_to[cnt])
  1596. continue;
  1597. /* Avoid races with quotaoff() */
  1598. if (!sb_has_quota_active(inode->i_sb, cnt))
  1599. continue;
  1600. is_valid[cnt] = 1;
  1601. transfer_from[cnt] = inode->i_dquot[cnt];
  1602. ret = check_idq(transfer_to[cnt], 1, warntype_to + cnt);
  1603. if (ret)
  1604. goto over_quota;
  1605. ret = check_bdq(transfer_to[cnt], space, 0, warntype_to + cnt);
  1606. if (ret)
  1607. goto over_quota;
  1608. }
  1609. /*
  1610. * Finally perform the needed transfer from transfer_from to transfer_to
  1611. */
  1612. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1613. if (!is_valid[cnt])
  1614. continue;
  1615. /* Due to IO error we might not have transfer_from[] structure */
  1616. if (transfer_from[cnt]) {
  1617. warntype_from_inodes[cnt] =
  1618. info_idq_free(transfer_from[cnt], 1);
  1619. warntype_from_space[cnt] =
  1620. info_bdq_free(transfer_from[cnt], space);
  1621. dquot_decr_inodes(transfer_from[cnt], 1);
  1622. dquot_decr_space(transfer_from[cnt], cur_space);
  1623. dquot_free_reserved_space(transfer_from[cnt],
  1624. rsv_space);
  1625. }
  1626. dquot_incr_inodes(transfer_to[cnt], 1);
  1627. dquot_incr_space(transfer_to[cnt], cur_space);
  1628. dquot_resv_space(transfer_to[cnt], rsv_space);
  1629. inode->i_dquot[cnt] = transfer_to[cnt];
  1630. }
  1631. spin_unlock(&dq_data_lock);
  1632. up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1633. mark_all_dquot_dirty(transfer_from);
  1634. mark_all_dquot_dirty(transfer_to);
  1635. flush_warnings(transfer_to, warntype_to);
  1636. flush_warnings(transfer_from, warntype_from_inodes);
  1637. flush_warnings(transfer_from, warntype_from_space);
  1638. /* Pass back references to put */
  1639. for (cnt = 0; cnt < MAXQUOTAS; cnt++)
  1640. if (is_valid[cnt])
  1641. transfer_to[cnt] = transfer_from[cnt];
  1642. return 0;
  1643. over_quota:
  1644. spin_unlock(&dq_data_lock);
  1645. up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1646. flush_warnings(transfer_to, warntype_to);
  1647. return ret;
  1648. }
  1649. EXPORT_SYMBOL(__dquot_transfer);
  1650. /* Wrapper for transferring ownership of an inode for uid/gid only
  1651. * Called from FSXXX_setattr()
  1652. */
  1653. int dquot_transfer(struct inode *inode, struct iattr *iattr)
  1654. {
  1655. struct dquot *transfer_to[MAXQUOTAS] = {};
  1656. struct super_block *sb = inode->i_sb;
  1657. int ret;
  1658. if (!dquot_active(inode))
  1659. return 0;
  1660. if (iattr->ia_valid & ATTR_UID && iattr->ia_uid != inode->i_uid)
  1661. transfer_to[USRQUOTA] = dqget(sb, iattr->ia_uid, USRQUOTA);
  1662. if (iattr->ia_valid & ATTR_GID && iattr->ia_gid != inode->i_gid)
  1663. transfer_to[GRPQUOTA] = dqget(sb, iattr->ia_gid, GRPQUOTA);
  1664. ret = __dquot_transfer(inode, transfer_to);
  1665. dqput_all(transfer_to);
  1666. return ret;
  1667. }
  1668. EXPORT_SYMBOL(dquot_transfer);
  1669. /*
  1670. * Write info of quota file to disk
  1671. */
  1672. int dquot_commit_info(struct super_block *sb, int type)
  1673. {
  1674. int ret;
  1675. struct quota_info *dqopt = sb_dqopt(sb);
  1676. mutex_lock(&dqopt->dqio_mutex);
  1677. ret = dqopt->ops[type]->write_file_info(sb, type);
  1678. mutex_unlock(&dqopt->dqio_mutex);
  1679. return ret;
  1680. }
  1681. EXPORT_SYMBOL(dquot_commit_info);
  1682. /*
  1683. * Definitions of diskquota operations.
  1684. */
  1685. const struct dquot_operations dquot_operations = {
  1686. .write_dquot = dquot_commit,
  1687. .acquire_dquot = dquot_acquire,
  1688. .release_dquot = dquot_release,
  1689. .mark_dirty = dquot_mark_dquot_dirty,
  1690. .write_info = dquot_commit_info,
  1691. .alloc_dquot = dquot_alloc,
  1692. .destroy_dquot = dquot_destroy,
  1693. };
  1694. EXPORT_SYMBOL(dquot_operations);
  1695. /*
  1696. * Generic helper for ->open on filesystems supporting disk quotas.
  1697. */
  1698. int dquot_file_open(struct inode *inode, struct file *file)
  1699. {
  1700. int error;
  1701. error = generic_file_open(inode, file);
  1702. if (!error && (file->f_mode & FMODE_WRITE))
  1703. dquot_initialize(inode);
  1704. return error;
  1705. }
  1706. EXPORT_SYMBOL(dquot_file_open);
  1707. /*
  1708. * Turn quota off on a device. type == -1 ==> quotaoff for all types (umount)
  1709. */
  1710. int dquot_disable(struct super_block *sb, int type, unsigned int flags)
  1711. {
  1712. int cnt, ret = 0;
  1713. struct quota_info *dqopt = sb_dqopt(sb);
  1714. struct inode *toputinode[MAXQUOTAS];
  1715. /* Cannot turn off usage accounting without turning off limits, or
  1716. * suspend quotas and simultaneously turn quotas off. */
  1717. if ((flags & DQUOT_USAGE_ENABLED && !(flags & DQUOT_LIMITS_ENABLED))
  1718. || (flags & DQUOT_SUSPENDED && flags & (DQUOT_LIMITS_ENABLED |
  1719. DQUOT_USAGE_ENABLED)))
  1720. return -EINVAL;
  1721. /* We need to serialize quota_off() for device */
  1722. mutex_lock(&dqopt->dqonoff_mutex);
  1723. /*
  1724. * Skip everything if there's nothing to do. We have to do this because
  1725. * sometimes we are called when fill_super() failed and calling
  1726. * sync_fs() in such cases does no good.
  1727. */
  1728. if (!sb_any_quota_loaded(sb)) {
  1729. mutex_unlock(&dqopt->dqonoff_mutex);
  1730. return 0;
  1731. }
  1732. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1733. toputinode[cnt] = NULL;
  1734. if (type != -1 && cnt != type)
  1735. continue;
  1736. if (!sb_has_quota_loaded(sb, cnt))
  1737. continue;
  1738. if (flags & DQUOT_SUSPENDED) {
  1739. spin_lock(&dq_state_lock);
  1740. dqopt->flags |=
  1741. dquot_state_flag(DQUOT_SUSPENDED, cnt);
  1742. spin_unlock(&dq_state_lock);
  1743. } else {
  1744. spin_lock(&dq_state_lock);
  1745. dqopt->flags &= ~dquot_state_flag(flags, cnt);
  1746. /* Turning off suspended quotas? */
  1747. if (!sb_has_quota_loaded(sb, cnt) &&
  1748. sb_has_quota_suspended(sb, cnt)) {
  1749. dqopt->flags &= ~dquot_state_flag(
  1750. DQUOT_SUSPENDED, cnt);
  1751. spin_unlock(&dq_state_lock);
  1752. iput(dqopt->files[cnt]);
  1753. dqopt->files[cnt] = NULL;
  1754. continue;
  1755. }
  1756. spin_unlock(&dq_state_lock);
  1757. }
  1758. /* We still have to keep quota loaded? */
  1759. if (sb_has_quota_loaded(sb, cnt) && !(flags & DQUOT_SUSPENDED))
  1760. continue;
  1761. /* Note: these are blocking operations */
  1762. drop_dquot_ref(sb, cnt);
  1763. invalidate_dquots(sb, cnt);
  1764. /*
  1765. * Now all dquots should be invalidated, all writes done so we
  1766. * should be only users of the info. No locks needed.
  1767. */
  1768. if (info_dirty(&dqopt->info[cnt]))
  1769. sb->dq_op->write_info(sb, cnt);
  1770. if (dqopt->ops[cnt]->free_file_info)
  1771. dqopt->ops[cnt]->free_file_info(sb, cnt);
  1772. put_quota_format(dqopt->info[cnt].dqi_format);
  1773. toputinode[cnt] = dqopt->files[cnt];
  1774. if (!sb_has_quota_loaded(sb, cnt))
  1775. dqopt->files[cnt] = NULL;
  1776. dqopt->info[cnt].dqi_flags = 0;
  1777. dqopt->info[cnt].dqi_igrace = 0;
  1778. dqopt->info[cnt].dqi_bgrace = 0;
  1779. dqopt->ops[cnt] = NULL;
  1780. }
  1781. mutex_unlock(&dqopt->dqonoff_mutex);
  1782. /* Skip syncing and setting flags if quota files are hidden */
  1783. if (dqopt->flags & DQUOT_QUOTA_SYS_FILE)
  1784. goto put_inodes;
  1785. /* Sync the superblock so that buffers with quota data are written to
  1786. * disk (and so userspace sees correct data afterwards). */
  1787. if (sb->s_op->sync_fs)
  1788. sb->s_op->sync_fs(sb, 1);
  1789. sync_blockdev(sb->s_bdev);
  1790. /* Now the quota files are just ordinary files and we can set the
  1791. * inode flags back. Moreover we discard the pagecache so that
  1792. * userspace sees the writes we did bypassing the pagecache. We
  1793. * must also discard the blockdev buffers so that we see the
  1794. * changes done by userspace on the next quotaon() */
  1795. for (cnt = 0; cnt < MAXQUOTAS; cnt++)
  1796. if (toputinode[cnt]) {
  1797. mutex_lock(&dqopt->dqonoff_mutex);
  1798. /* If quota was reenabled in the meantime, we have
  1799. * nothing to do */
  1800. if (!sb_has_quota_loaded(sb, cnt)) {
  1801. mutex_lock_nested(&toputinode[cnt]->i_mutex,
  1802. I_MUTEX_QUOTA);
  1803. toputinode[cnt]->i_flags &= ~(S_IMMUTABLE |
  1804. S_NOATIME | S_NOQUOTA);
  1805. truncate_inode_pages(&toputinode[cnt]->i_data,
  1806. 0);
  1807. mutex_unlock(&toputinode[cnt]->i_mutex);
  1808. mark_inode_dirty_sync(toputinode[cnt]);
  1809. }
  1810. mutex_unlock(&dqopt->dqonoff_mutex);
  1811. }
  1812. if (sb->s_bdev)
  1813. invalidate_bdev(sb->s_bdev);
  1814. put_inodes:
  1815. for (cnt = 0; cnt < MAXQUOTAS; cnt++)
  1816. if (toputinode[cnt]) {
  1817. /* On remount RO, we keep the inode pointer so that we
  1818. * can reenable quota on the subsequent remount RW. We
  1819. * have to check 'flags' variable and not use sb_has_
  1820. * function because another quotaon / quotaoff could
  1821. * change global state before we got here. We refuse
  1822. * to suspend quotas when there is pending delete on
  1823. * the quota file... */
  1824. if (!(flags & DQUOT_SUSPENDED))
  1825. iput(toputinode[cnt]);
  1826. else if (!toputinode[cnt]->i_nlink)
  1827. ret = -EBUSY;
  1828. }
  1829. return ret;
  1830. }
  1831. EXPORT_SYMBOL(dquot_disable);
  1832. int dquot_quota_off(struct super_block *sb, int type)
  1833. {
  1834. return dquot_disable(sb, type,
  1835. DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED);
  1836. }
  1837. EXPORT_SYMBOL(dquot_quota_off);
  1838. /*
  1839. * Turn quotas on on a device
  1840. */
  1841. /*
  1842. * Helper function to turn quotas on when we already have the inode of
  1843. * quota file and no quota information is loaded.
  1844. */
  1845. static int vfs_load_quota_inode(struct inode *inode, int type, int format_id,
  1846. unsigned int flags)
  1847. {
  1848. struct quota_format_type *fmt = find_quota_format(format_id);
  1849. struct super_block *sb = inode->i_sb;
  1850. struct quota_info *dqopt = sb_dqopt(sb);
  1851. int error;
  1852. int oldflags = -1;
  1853. if (!fmt)
  1854. return -ESRCH;
  1855. if (!S_ISREG(inode->i_mode)) {
  1856. error = -EACCES;
  1857. goto out_fmt;
  1858. }
  1859. if (IS_RDONLY(inode)) {
  1860. error = -EROFS;
  1861. goto out_fmt;
  1862. }
  1863. if (!sb->s_op->quota_write || !sb->s_op->quota_read) {
  1864. error = -EINVAL;
  1865. goto out_fmt;
  1866. }
  1867. /* Usage always has to be set... */
  1868. if (!(flags & DQUOT_USAGE_ENABLED)) {
  1869. error = -EINVAL;
  1870. goto out_fmt;
  1871. }
  1872. if (!(dqopt->flags & DQUOT_QUOTA_SYS_FILE)) {
  1873. /* As we bypass the pagecache we must now flush all the
  1874. * dirty data and invalidate caches so that kernel sees
  1875. * changes from userspace. It is not enough to just flush
  1876. * the quota file since if blocksize < pagesize, invalidation
  1877. * of the cache could fail because of other unrelated dirty
  1878. * data */
  1879. sync_filesystem(sb);
  1880. invalidate_bdev(sb->s_bdev);
  1881. }
  1882. mutex_lock(&dqopt->dqonoff_mutex);
  1883. if (sb_has_quota_loaded(sb, type)) {
  1884. error = -EBUSY;
  1885. goto out_lock;
  1886. }
  1887. if (!(dqopt->flags & DQUOT_QUOTA_SYS_FILE)) {
  1888. /* We don't want quota and atime on quota files (deadlocks
  1889. * possible) Also nobody should write to the file - we use
  1890. * special IO operations which ignore the immutable bit. */
  1891. mutex_lock_nested(&inode->i_mutex, I_MUTEX_QUOTA);
  1892. oldflags = inode->i_flags & (S_NOATIME | S_IMMUTABLE |
  1893. S_NOQUOTA);
  1894. inode->i_flags |= S_NOQUOTA | S_NOATIME | S_IMMUTABLE;
  1895. mutex_unlock(&inode->i_mutex);
  1896. /*
  1897. * When S_NOQUOTA is set, remove dquot references as no more
  1898. * references can be added
  1899. */
  1900. __dquot_drop(inode);
  1901. }
  1902. error = -EIO;
  1903. dqopt->files[type] = igrab(inode);
  1904. if (!dqopt->files[type])
  1905. goto out_lock;
  1906. error = -EINVAL;
  1907. if (!fmt->qf_ops->check_quota_file(sb, type))
  1908. goto out_file_init;
  1909. dqopt->ops[type] = fmt->qf_ops;
  1910. dqopt->info[type].dqi_format = fmt;
  1911. dqopt->info[type].dqi_fmt_id = format_id;
  1912. INIT_LIST_HEAD(&dqopt->info[type].dqi_dirty_list);
  1913. mutex_lock(&dqopt->dqio_mutex);
  1914. error = dqopt->ops[type]->read_file_info(sb, type);
  1915. if (error < 0) {
  1916. mutex_unlock(&dqopt->dqio_mutex);
  1917. goto out_file_init;
  1918. }
  1919. mutex_unlock(&dqopt->dqio_mutex);
  1920. spin_lock(&dq_state_lock);
  1921. dqopt->flags |= dquot_state_flag(flags, type);
  1922. spin_unlock(&dq_state_lock);
  1923. add_dquot_ref(sb, type);
  1924. mutex_unlock(&dqopt->dqonoff_mutex);
  1925. return 0;
  1926. out_file_init:
  1927. dqopt->files[type] = NULL;
  1928. iput(inode);
  1929. out_lock:
  1930. if (oldflags != -1) {
  1931. mutex_lock_nested(&inode->i_mutex, I_MUTEX_QUOTA);
  1932. /* Set the flags back (in the case of accidental quotaon()
  1933. * on a wrong file we don't want to mess up the flags) */
  1934. inode->i_flags &= ~(S_NOATIME | S_NOQUOTA | S_IMMUTABLE);
  1935. inode->i_flags |= oldflags;
  1936. mutex_unlock(&inode->i_mutex);
  1937. }
  1938. mutex_unlock(&dqopt->dqonoff_mutex);
  1939. out_fmt:
  1940. put_quota_format(fmt);
  1941. return error;
  1942. }
  1943. /* Reenable quotas on remount RW */
  1944. int dquot_resume(struct super_block *sb, int type)
  1945. {
  1946. struct quota_info *dqopt = sb_dqopt(sb);
  1947. struct inode *inode;
  1948. int ret = 0, cnt;
  1949. unsigned int flags;
  1950. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1951. if (type != -1 && cnt != type)
  1952. continue;
  1953. mutex_lock(&dqopt->dqonoff_mutex);
  1954. if (!sb_has_quota_suspended(sb, cnt)) {
  1955. mutex_unlock(&dqopt->dqonoff_mutex);
  1956. continue;
  1957. }
  1958. inode = dqopt->files[cnt];
  1959. dqopt->files[cnt] = NULL;
  1960. spin_lock(&dq_state_lock);
  1961. flags = dqopt->flags & dquot_state_flag(DQUOT_USAGE_ENABLED |
  1962. DQUOT_LIMITS_ENABLED,
  1963. cnt);
  1964. dqopt->flags &= ~dquot_state_flag(DQUOT_STATE_FLAGS, cnt);
  1965. spin_unlock(&dq_state_lock);
  1966. mutex_unlock(&dqopt->dqonoff_mutex);
  1967. flags = dquot_generic_flag(flags, cnt);
  1968. ret = vfs_load_quota_inode(inode, cnt,
  1969. dqopt->info[cnt].dqi_fmt_id, flags);
  1970. iput(inode);
  1971. }
  1972. return ret;
  1973. }
  1974. EXPORT_SYMBOL(dquot_resume);
  1975. int dquot_quota_on(struct super_block *sb, int type, int format_id,
  1976. struct path *path)
  1977. {
  1978. int error = security_quota_on(path->dentry);
  1979. if (error)
  1980. return error;
  1981. /* Quota file not on the same filesystem? */
  1982. if (path->mnt->mnt_sb != sb)
  1983. error = -EXDEV;
  1984. else
  1985. error = vfs_load_quota_inode(path->dentry->d_inode, type,
  1986. format_id, DQUOT_USAGE_ENABLED |
  1987. DQUOT_LIMITS_ENABLED);
  1988. return error;
  1989. }
  1990. EXPORT_SYMBOL(dquot_quota_on);
  1991. /*
  1992. * More powerful function for turning on quotas allowing setting
  1993. * of individual quota flags
  1994. */
  1995. int dquot_enable(struct inode *inode, int type, int format_id,
  1996. unsigned int flags)
  1997. {
  1998. int ret = 0;
  1999. struct super_block *sb = inode->i_sb;
  2000. struct quota_info *dqopt = sb_dqopt(sb);
  2001. /* Just unsuspend quotas? */
  2002. BUG_ON(flags & DQUOT_SUSPENDED);
  2003. if (!flags)
  2004. return 0;
  2005. /* Just updating flags needed? */
  2006. if (sb_has_quota_loaded(sb, type)) {
  2007. mutex_lock(&dqopt->dqonoff_mutex);
  2008. /* Now do a reliable test... */
  2009. if (!sb_has_quota_loaded(sb, type)) {
  2010. mutex_unlock(&dqopt->dqonoff_mutex);
  2011. goto load_quota;
  2012. }
  2013. if (flags & DQUOT_USAGE_ENABLED &&
  2014. sb_has_quota_usage_enabled(sb, type)) {
  2015. ret = -EBUSY;
  2016. goto out_lock;
  2017. }
  2018. if (flags & DQUOT_LIMITS_ENABLED &&
  2019. sb_has_quota_limits_enabled(sb, type)) {
  2020. ret = -EBUSY;
  2021. goto out_lock;
  2022. }
  2023. spin_lock(&dq_state_lock);
  2024. sb_dqopt(sb)->flags |= dquot_state_flag(flags, type);
  2025. spin_unlock(&dq_state_lock);
  2026. out_lock:
  2027. mutex_unlock(&dqopt->dqonoff_mutex);
  2028. return ret;
  2029. }
  2030. load_quota:
  2031. return vfs_load_quota_inode(inode, type, format_id, flags);
  2032. }
  2033. EXPORT_SYMBOL(dquot_enable);
  2034. /*
  2035. * This function is used when filesystem needs to initialize quotas
  2036. * during mount time.
  2037. */
  2038. int dquot_quota_on_mount(struct super_block *sb, char *qf_name,
  2039. int format_id, int type)
  2040. {
  2041. struct dentry *dentry;
  2042. int error;
  2043. mutex_lock(&sb->s_root->d_inode->i_mutex);
  2044. dentry = lookup_one_len(qf_name, sb->s_root, strlen(qf_name));
  2045. mutex_unlock(&sb->s_root->d_inode->i_mutex);
  2046. if (IS_ERR(dentry))
  2047. return PTR_ERR(dentry);
  2048. if (!dentry->d_inode) {
  2049. error = -ENOENT;
  2050. goto out;
  2051. }
  2052. error = security_quota_on(dentry);
  2053. if (!error)
  2054. error = vfs_load_quota_inode(dentry->d_inode, type, format_id,
  2055. DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED);
  2056. out:
  2057. dput(dentry);
  2058. return error;
  2059. }
  2060. EXPORT_SYMBOL(dquot_quota_on_mount);
  2061. static inline qsize_t qbtos(qsize_t blocks)
  2062. {
  2063. return blocks << QIF_DQBLKSIZE_BITS;
  2064. }
  2065. static inline qsize_t stoqb(qsize_t space)
  2066. {
  2067. return (space + QIF_DQBLKSIZE - 1) >> QIF_DQBLKSIZE_BITS;
  2068. }
  2069. /* Generic routine for getting common part of quota structure */
  2070. static void do_get_dqblk(struct dquot *dquot, struct fs_disk_quota *di)
  2071. {
  2072. struct mem_dqblk *dm = &dquot->dq_dqb;
  2073. memset(di, 0, sizeof(*di));
  2074. di->d_version = FS_DQUOT_VERSION;
  2075. di->d_flags = dquot->dq_type == USRQUOTA ?
  2076. FS_USER_QUOTA : FS_GROUP_QUOTA;
  2077. di->d_id = dquot->dq_id;
  2078. spin_lock(&dq_data_lock);
  2079. di->d_blk_hardlimit = stoqb(dm->dqb_bhardlimit);
  2080. di->d_blk_softlimit = stoqb(dm->dqb_bsoftlimit);
  2081. di->d_ino_hardlimit = dm->dqb_ihardlimit;
  2082. di->d_ino_softlimit = dm->dqb_isoftlimit;
  2083. di->d_bcount = dm->dqb_curspace + dm->dqb_rsvspace;
  2084. di->d_icount = dm->dqb_curinodes;
  2085. di->d_btimer = dm->dqb_btime;
  2086. di->d_itimer = dm->dqb_itime;
  2087. spin_unlock(&dq_data_lock);
  2088. }
  2089. int dquot_get_dqblk(struct super_block *sb, int type, qid_t id,
  2090. struct fs_disk_quota *di)
  2091. {
  2092. struct dquot *dquot;
  2093. dquot = dqget(sb, id, type);
  2094. if (!dquot)
  2095. return -ESRCH;
  2096. do_get_dqblk(dquot, di);
  2097. dqput(dquot);
  2098. return 0;
  2099. }
  2100. EXPORT_SYMBOL(dquot_get_dqblk);
  2101. #define VFS_FS_DQ_MASK \
  2102. (FS_DQ_BCOUNT | FS_DQ_BSOFT | FS_DQ_BHARD | \
  2103. FS_DQ_ICOUNT | FS_DQ_ISOFT | FS_DQ_IHARD | \
  2104. FS_DQ_BTIMER | FS_DQ_ITIMER)
  2105. /* Generic routine for setting common part of quota structure */
  2106. static int do_set_dqblk(struct dquot *dquot, struct fs_disk_quota *di)
  2107. {
  2108. struct mem_dqblk *dm = &dquot->dq_dqb;
  2109. int check_blim = 0, check_ilim = 0;
  2110. struct mem_dqinfo *dqi = &sb_dqopt(dquot->dq_sb)->info[dquot->dq_type];
  2111. if (di->d_fieldmask & ~VFS_FS_DQ_MASK)
  2112. return -EINVAL;
  2113. if (((di->d_fieldmask & FS_DQ_BSOFT) &&
  2114. (di->d_blk_softlimit > dqi->dqi_maxblimit)) ||
  2115. ((di->d_fieldmask & FS_DQ_BHARD) &&
  2116. (di->d_blk_hardlimit > dqi->dqi_maxblimit)) ||
  2117. ((di->d_fieldmask & FS_DQ_ISOFT) &&
  2118. (di->d_ino_softlimit > dqi->dqi_maxilimit)) ||
  2119. ((di->d_fieldmask & FS_DQ_IHARD) &&
  2120. (di->d_ino_hardlimit > dqi->dqi_maxilimit)))
  2121. return -ERANGE;
  2122. spin_lock(&dq_data_lock);
  2123. if (di->d_fieldmask & FS_DQ_BCOUNT) {
  2124. dm->dqb_curspace = di->d_bcount - dm->dqb_rsvspace;
  2125. check_blim = 1;
  2126. set_bit(DQ_LASTSET_B + QIF_SPACE_B, &dquot->dq_flags);
  2127. }
  2128. if (di->d_fieldmask & FS_DQ_BSOFT)
  2129. dm->dqb_bsoftlimit = qbtos(di->d_blk_softlimit);
  2130. if (di->d_fieldmask & FS_DQ_BHARD)
  2131. dm->dqb_bhardlimit = qbtos(di->d_blk_hardlimit);
  2132. if (di->d_fieldmask & (FS_DQ_BSOFT | FS_DQ_BHARD)) {
  2133. check_blim = 1;
  2134. set_bit(DQ_LASTSET_B + QIF_BLIMITS_B, &dquot->dq_flags);
  2135. }
  2136. if (di->d_fieldmask & FS_DQ_ICOUNT) {
  2137. dm->dqb_curinodes = di->d_icount;
  2138. check_ilim = 1;
  2139. set_bit(DQ_LASTSET_B + QIF_INODES_B, &dquot->dq_flags);
  2140. }
  2141. if (di->d_fieldmask & FS_DQ_ISOFT)
  2142. dm->dqb_isoftlimit = di->d_ino_softlimit;
  2143. if (di->d_fieldmask & FS_DQ_IHARD)
  2144. dm->dqb_ihardlimit = di->d_ino_hardlimit;
  2145. if (di->d_fieldmask & (FS_DQ_ISOFT | FS_DQ_IHARD)) {
  2146. check_ilim = 1;
  2147. set_bit(DQ_LASTSET_B + QIF_ILIMITS_B, &dquot->dq_flags);
  2148. }
  2149. if (di->d_fieldmask & FS_DQ_BTIMER) {
  2150. dm->dqb_btime = di->d_btimer;
  2151. check_blim = 1;
  2152. set_bit(DQ_LASTSET_B + QIF_BTIME_B, &dquot->dq_flags);
  2153. }
  2154. if (di->d_fieldmask & FS_DQ_ITIMER) {
  2155. dm->dqb_itime = di->d_itimer;
  2156. check_ilim = 1;
  2157. set_bit(DQ_LASTSET_B + QIF_ITIME_B, &dquot->dq_flags);
  2158. }
  2159. if (check_blim) {
  2160. if (!dm->dqb_bsoftlimit ||
  2161. dm->dqb_curspace < dm->dqb_bsoftlimit) {
  2162. dm->dqb_btime = 0;
  2163. clear_bit(DQ_BLKS_B, &dquot->dq_flags);
  2164. } else if (!(di->d_fieldmask & FS_DQ_BTIMER))
  2165. /* Set grace only if user hasn't provided his own... */
  2166. dm->dqb_btime = get_seconds() + dqi->dqi_bgrace;
  2167. }
  2168. if (check_ilim) {
  2169. if (!dm->dqb_isoftlimit ||
  2170. dm->dqb_curinodes < dm->dqb_isoftlimit) {
  2171. dm->dqb_itime = 0;
  2172. clear_bit(DQ_INODES_B, &dquot->dq_flags);
  2173. } else if (!(di->d_fieldmask & FS_DQ_ITIMER))
  2174. /* Set grace only if user hasn't provided his own... */
  2175. dm->dqb_itime = get_seconds() + dqi->dqi_igrace;
  2176. }
  2177. if (dm->dqb_bhardlimit || dm->dqb_bsoftlimit || dm->dqb_ihardlimit ||
  2178. dm->dqb_isoftlimit)
  2179. clear_bit(DQ_FAKE_B, &dquot->dq_flags);
  2180. else
  2181. set_bit(DQ_FAKE_B, &dquot->dq_flags);
  2182. spin_unlock(&dq_data_lock);
  2183. mark_dquot_dirty(dquot);
  2184. return 0;
  2185. }
  2186. int dquot_set_dqblk(struct super_block *sb, int type, qid_t id,
  2187. struct fs_disk_quota *di)
  2188. {
  2189. struct dquot *dquot;
  2190. int rc;
  2191. dquot = dqget(sb, id, type);
  2192. if (!dquot) {
  2193. rc = -ESRCH;
  2194. goto out;
  2195. }
  2196. rc = do_set_dqblk(dquot, di);
  2197. dqput(dquot);
  2198. out:
  2199. return rc;
  2200. }
  2201. EXPORT_SYMBOL(dquot_set_dqblk);
  2202. /* Generic routine for getting common part of quota file information */
  2203. int dquot_get_dqinfo(struct super_block *sb, int type, struct if_dqinfo *ii)
  2204. {
  2205. struct mem_dqinfo *mi;
  2206. mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
  2207. if (!sb_has_quota_active(sb, type)) {
  2208. mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
  2209. return -ESRCH;
  2210. }
  2211. mi = sb_dqopt(sb)->info + type;
  2212. spin_lock(&dq_data_lock);
  2213. ii->dqi_bgrace = mi->dqi_bgrace;
  2214. ii->dqi_igrace = mi->dqi_igrace;
  2215. ii->dqi_flags = mi->dqi_flags & DQF_MASK;
  2216. ii->dqi_valid = IIF_ALL;
  2217. spin_unlock(&dq_data_lock);
  2218. mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
  2219. return 0;
  2220. }
  2221. EXPORT_SYMBOL(dquot_get_dqinfo);
  2222. /* Generic routine for setting common part of quota file information */
  2223. int dquot_set_dqinfo(struct super_block *sb, int type, struct if_dqinfo *ii)
  2224. {
  2225. struct mem_dqinfo *mi;
  2226. int err = 0;
  2227. mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
  2228. if (!sb_has_quota_active(sb, type)) {
  2229. err = -ESRCH;
  2230. goto out;
  2231. }
  2232. mi = sb_dqopt(sb)->info + type;
  2233. spin_lock(&dq_data_lock);
  2234. if (ii->dqi_valid & IIF_BGRACE)
  2235. mi->dqi_bgrace = ii->dqi_bgrace;
  2236. if (ii->dqi_valid & IIF_IGRACE)
  2237. mi->dqi_igrace = ii->dqi_igrace;
  2238. if (ii->dqi_valid & IIF_FLAGS)
  2239. mi->dqi_flags = (mi->dqi_flags & ~DQF_MASK) |
  2240. (ii->dqi_flags & DQF_MASK);
  2241. spin_unlock(&dq_data_lock);
  2242. mark_info_dirty(sb, type);
  2243. /* Force write to disk */
  2244. sb->dq_op->write_info(sb, type);
  2245. out:
  2246. mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
  2247. return err;
  2248. }
  2249. EXPORT_SYMBOL(dquot_set_dqinfo);
  2250. const struct quotactl_ops dquot_quotactl_ops = {
  2251. .quota_on = dquot_quota_on,
  2252. .quota_off = dquot_quota_off,
  2253. .quota_sync = dquot_quota_sync,
  2254. .get_info = dquot_get_dqinfo,
  2255. .set_info = dquot_set_dqinfo,
  2256. .get_dqblk = dquot_get_dqblk,
  2257. .set_dqblk = dquot_set_dqblk
  2258. };
  2259. EXPORT_SYMBOL(dquot_quotactl_ops);
  2260. static int do_proc_dqstats(struct ctl_table *table, int write,
  2261. void __user *buffer, size_t *lenp, loff_t *ppos)
  2262. {
  2263. unsigned int type = (int *)table->data - dqstats.stat;
  2264. /* Update global table */
  2265. dqstats.stat[type] =
  2266. percpu_counter_sum_positive(&dqstats.counter[type]);
  2267. return proc_dointvec(table, write, buffer, lenp, ppos);
  2268. }
  2269. static ctl_table fs_dqstats_table[] = {
  2270. {
  2271. .procname = "lookups",
  2272. .data = &dqstats.stat[DQST_LOOKUPS],
  2273. .maxlen = sizeof(int),
  2274. .mode = 0444,
  2275. .proc_handler = do_proc_dqstats,
  2276. },
  2277. {
  2278. .procname = "drops",
  2279. .data = &dqstats.stat[DQST_DROPS],
  2280. .maxlen = sizeof(int),
  2281. .mode = 0444,
  2282. .proc_handler = do_proc_dqstats,
  2283. },
  2284. {
  2285. .procname = "reads",
  2286. .data = &dqstats.stat[DQST_READS],
  2287. .maxlen = sizeof(int),
  2288. .mode = 0444,
  2289. .proc_handler = do_proc_dqstats,
  2290. },
  2291. {
  2292. .procname = "writes",
  2293. .data = &dqstats.stat[DQST_WRITES],
  2294. .maxlen = sizeof(int),
  2295. .mode = 0444,
  2296. .proc_handler = do_proc_dqstats,
  2297. },
  2298. {
  2299. .procname = "cache_hits",
  2300. .data = &dqstats.stat[DQST_CACHE_HITS],
  2301. .maxlen = sizeof(int),
  2302. .mode = 0444,
  2303. .proc_handler = do_proc_dqstats,
  2304. },
  2305. {
  2306. .procname = "allocated_dquots",
  2307. .data = &dqstats.stat[DQST_ALLOC_DQUOTS],
  2308. .maxlen = sizeof(int),
  2309. .mode = 0444,
  2310. .proc_handler = do_proc_dqstats,
  2311. },
  2312. {
  2313. .procname = "free_dquots",
  2314. .data = &dqstats.stat[DQST_FREE_DQUOTS],
  2315. .maxlen = sizeof(int),
  2316. .mode = 0444,
  2317. .proc_handler = do_proc_dqstats,
  2318. },
  2319. {
  2320. .procname = "syncs",
  2321. .data = &dqstats.stat[DQST_SYNCS],
  2322. .maxlen = sizeof(int),
  2323. .mode = 0444,
  2324. .proc_handler = do_proc_dqstats,
  2325. },
  2326. #ifdef CONFIG_PRINT_QUOTA_WARNING
  2327. {
  2328. .procname = "warnings",
  2329. .data = &flag_print_warnings,
  2330. .maxlen = sizeof(int),
  2331. .mode = 0644,
  2332. .proc_handler = proc_dointvec,
  2333. },
  2334. #endif
  2335. { },
  2336. };
  2337. static ctl_table fs_table[] = {
  2338. {
  2339. .procname = "quota",
  2340. .mode = 0555,
  2341. .child = fs_dqstats_table,
  2342. },
  2343. { },
  2344. };
  2345. static ctl_table sys_table[] = {
  2346. {
  2347. .procname = "fs",
  2348. .mode = 0555,
  2349. .child = fs_table,
  2350. },
  2351. { },
  2352. };
  2353. static int __init dquot_init(void)
  2354. {
  2355. int i, ret;
  2356. unsigned long nr_hash, order;
  2357. printk(KERN_NOTICE "VFS: Disk quotas %s\n", __DQUOT_VERSION__);
  2358. register_sysctl_table(sys_table);
  2359. dquot_cachep = kmem_cache_create("dquot",
  2360. sizeof(struct dquot), sizeof(unsigned long) * 4,
  2361. (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
  2362. SLAB_MEM_SPREAD|SLAB_PANIC),
  2363. NULL);
  2364. order = 0;
  2365. dquot_hash = (struct hlist_head *)__get_free_pages(GFP_ATOMIC, order);
  2366. if (!dquot_hash)
  2367. panic("Cannot create dquot hash table");
  2368. for (i = 0; i < _DQST_DQSTAT_LAST; i++) {
  2369. ret = percpu_counter_init(&dqstats.counter[i], 0);
  2370. if (ret)
  2371. panic("Cannot create dquot stat counters");
  2372. }
  2373. /* Find power-of-two hlist_heads which can fit into allocation */
  2374. nr_hash = (1UL << order) * PAGE_SIZE / sizeof(struct hlist_head);
  2375. dq_hash_bits = 0;
  2376. do {
  2377. dq_hash_bits++;
  2378. } while (nr_hash >> dq_hash_bits);
  2379. dq_hash_bits--;
  2380. nr_hash = 1UL << dq_hash_bits;
  2381. dq_hash_mask = nr_hash - 1;
  2382. for (i = 0; i < nr_hash; i++)
  2383. INIT_HLIST_HEAD(dquot_hash + i);
  2384. printk("Dquot-cache hash table entries: %ld (order %ld, %ld bytes)\n",
  2385. nr_hash, order, (PAGE_SIZE << order));
  2386. register_shrinker(&dqcache_shrinker);
  2387. return 0;
  2388. }
  2389. module_init(dquot_init);