dquot.c 79 KB

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