dquot.c 71 KB

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