dquot.c 73 KB

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