dquot.c 79 KB

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