dquot.c 79 KB

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