dquot.c 77 KB

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