dquot.c 73 KB

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