dquot.c 71 KB

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