dquot.c 73 KB

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