dquot.c 79 KB

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