dquot.c 78 KB

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