dquot.c 75 KB

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