dquot.c 75 KB

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