dquot.c 72 KB

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