dquot.c 70 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604
  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, dqstats structure containing statistics about the lists
  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. static char *quotatypes[] = INITQFNAMES;
  131. static struct quota_format_type *quota_formats; /* List of registered formats */
  132. static struct quota_module_name module_names[] = INIT_QUOTA_MODULE_NAMES;
  133. /* SLAB cache for dquot structures */
  134. static struct kmem_cache *dquot_cachep;
  135. int register_quota_format(struct quota_format_type *fmt)
  136. {
  137. spin_lock(&dq_list_lock);
  138. fmt->qf_next = quota_formats;
  139. quota_formats = fmt;
  140. spin_unlock(&dq_list_lock);
  141. return 0;
  142. }
  143. EXPORT_SYMBOL(register_quota_format);
  144. void unregister_quota_format(struct quota_format_type *fmt)
  145. {
  146. struct quota_format_type **actqf;
  147. spin_lock(&dq_list_lock);
  148. for (actqf = &quota_formats; *actqf && *actqf != fmt;
  149. actqf = &(*actqf)->qf_next)
  150. ;
  151. if (*actqf)
  152. *actqf = (*actqf)->qf_next;
  153. spin_unlock(&dq_list_lock);
  154. }
  155. EXPORT_SYMBOL(unregister_quota_format);
  156. static struct quota_format_type *find_quota_format(int id)
  157. {
  158. struct quota_format_type *actqf;
  159. spin_lock(&dq_list_lock);
  160. for (actqf = quota_formats; actqf && actqf->qf_fmt_id != id;
  161. actqf = actqf->qf_next)
  162. ;
  163. if (!actqf || !try_module_get(actqf->qf_owner)) {
  164. int qm;
  165. spin_unlock(&dq_list_lock);
  166. for (qm = 0; module_names[qm].qm_fmt_id &&
  167. module_names[qm].qm_fmt_id != id; qm++)
  168. ;
  169. if (!module_names[qm].qm_fmt_id ||
  170. request_module(module_names[qm].qm_mod_name))
  171. return NULL;
  172. spin_lock(&dq_list_lock);
  173. for (actqf = quota_formats; actqf && actqf->qf_fmt_id != id;
  174. actqf = actqf->qf_next)
  175. ;
  176. if (actqf && !try_module_get(actqf->qf_owner))
  177. actqf = NULL;
  178. }
  179. spin_unlock(&dq_list_lock);
  180. return actqf;
  181. }
  182. static void put_quota_format(struct quota_format_type *fmt)
  183. {
  184. module_put(fmt->qf_owner);
  185. }
  186. /*
  187. * Dquot List Management:
  188. * The quota code uses three lists for dquot management: the inuse_list,
  189. * free_dquots, and dquot_hash[] array. A single dquot structure may be
  190. * on all three lists, depending on its current state.
  191. *
  192. * All dquots are placed to the end of inuse_list when first created, and this
  193. * list is used for invalidate operation, which must look at every dquot.
  194. *
  195. * Unused dquots (dq_count == 0) are added to the free_dquots list when freed,
  196. * and this list is searched whenever we need an available dquot. Dquots are
  197. * removed from the list as soon as they are used again, and
  198. * dqstats.free_dquots gives the number of dquots on the list. When
  199. * dquot is invalidated it's completely released from memory.
  200. *
  201. * Dquots with a specific identity (device, type and id) are placed on
  202. * one of the dquot_hash[] hash chains. The provides an efficient search
  203. * mechanism to locate a specific dquot.
  204. */
  205. static LIST_HEAD(inuse_list);
  206. static LIST_HEAD(free_dquots);
  207. static unsigned int dq_hash_bits, dq_hash_mask;
  208. static struct hlist_head *dquot_hash;
  209. struct dqstats dqstats;
  210. EXPORT_SYMBOL(dqstats);
  211. static qsize_t inode_get_rsv_space(struct inode *inode);
  212. static void __dquot_initialize(struct inode *inode, int type);
  213. static inline unsigned int
  214. hashfn(const struct super_block *sb, unsigned int id, int type)
  215. {
  216. unsigned long tmp;
  217. tmp = (((unsigned long)sb>>L1_CACHE_SHIFT) ^ id) * (MAXQUOTAS - type);
  218. return (tmp + (tmp >> dq_hash_bits)) & dq_hash_mask;
  219. }
  220. /*
  221. * Following list functions expect dq_list_lock to be held
  222. */
  223. static inline void insert_dquot_hash(struct dquot *dquot)
  224. {
  225. struct hlist_head *head;
  226. head = dquot_hash + hashfn(dquot->dq_sb, dquot->dq_id, dquot->dq_type);
  227. hlist_add_head(&dquot->dq_hash, head);
  228. }
  229. static inline void remove_dquot_hash(struct dquot *dquot)
  230. {
  231. hlist_del_init(&dquot->dq_hash);
  232. }
  233. static struct dquot *find_dquot(unsigned int hashent, struct super_block *sb,
  234. unsigned int id, int type)
  235. {
  236. struct hlist_node *node;
  237. struct dquot *dquot;
  238. hlist_for_each (node, dquot_hash+hashent) {
  239. dquot = hlist_entry(node, struct dquot, dq_hash);
  240. if (dquot->dq_sb == sb && dquot->dq_id == id &&
  241. dquot->dq_type == type)
  242. return dquot;
  243. }
  244. return NULL;
  245. }
  246. /* Add a dquot to the tail of the free list */
  247. static inline void put_dquot_last(struct dquot *dquot)
  248. {
  249. list_add_tail(&dquot->dq_free, &free_dquots);
  250. dqstats.free_dquots++;
  251. }
  252. static inline void remove_free_dquot(struct dquot *dquot)
  253. {
  254. if (list_empty(&dquot->dq_free))
  255. return;
  256. list_del_init(&dquot->dq_free);
  257. dqstats.free_dquots--;
  258. }
  259. static inline void put_inuse(struct dquot *dquot)
  260. {
  261. /* We add to the back of inuse list so we don't have to restart
  262. * when traversing this list and we block */
  263. list_add_tail(&dquot->dq_inuse, &inuse_list);
  264. dqstats.allocated_dquots++;
  265. }
  266. static inline void remove_inuse(struct dquot *dquot)
  267. {
  268. dqstats.allocated_dquots--;
  269. list_del(&dquot->dq_inuse);
  270. }
  271. /*
  272. * End of list functions needing dq_list_lock
  273. */
  274. static void wait_on_dquot(struct dquot *dquot)
  275. {
  276. mutex_lock(&dquot->dq_lock);
  277. mutex_unlock(&dquot->dq_lock);
  278. }
  279. static inline int dquot_dirty(struct dquot *dquot)
  280. {
  281. return test_bit(DQ_MOD_B, &dquot->dq_flags);
  282. }
  283. static inline int mark_dquot_dirty(struct dquot *dquot)
  284. {
  285. return dquot->dq_sb->dq_op->mark_dirty(dquot);
  286. }
  287. /* Mark dquot dirty in atomic manner, and return it's old dirty flag state */
  288. int dquot_mark_dquot_dirty(struct dquot *dquot)
  289. {
  290. int ret = 1;
  291. /* If quota is dirty already, we don't have to acquire dq_list_lock */
  292. if (test_bit(DQ_MOD_B, &dquot->dq_flags))
  293. return 1;
  294. spin_lock(&dq_list_lock);
  295. if (!test_and_set_bit(DQ_MOD_B, &dquot->dq_flags)) {
  296. list_add(&dquot->dq_dirty, &sb_dqopt(dquot->dq_sb)->
  297. info[dquot->dq_type].dqi_dirty_list);
  298. ret = 0;
  299. }
  300. spin_unlock(&dq_list_lock);
  301. return ret;
  302. }
  303. EXPORT_SYMBOL(dquot_mark_dquot_dirty);
  304. /* Dirtify all the dquots - this can block when journalling */
  305. static inline int mark_all_dquot_dirty(struct dquot * const *dquot)
  306. {
  307. int ret, err, cnt;
  308. ret = err = 0;
  309. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  310. if (dquot[cnt])
  311. /* Even in case of error we have to continue */
  312. ret = mark_dquot_dirty(dquot[cnt]);
  313. if (!err)
  314. err = ret;
  315. }
  316. return err;
  317. }
  318. static inline void dqput_all(struct dquot **dquot)
  319. {
  320. unsigned int cnt;
  321. for (cnt = 0; cnt < MAXQUOTAS; cnt++)
  322. dqput(dquot[cnt]);
  323. }
  324. /* This function needs dq_list_lock */
  325. static inline int clear_dquot_dirty(struct dquot *dquot)
  326. {
  327. if (!test_and_clear_bit(DQ_MOD_B, &dquot->dq_flags))
  328. return 0;
  329. list_del_init(&dquot->dq_dirty);
  330. return 1;
  331. }
  332. void mark_info_dirty(struct super_block *sb, int type)
  333. {
  334. set_bit(DQF_INFO_DIRTY_B, &sb_dqopt(sb)->info[type].dqi_flags);
  335. }
  336. EXPORT_SYMBOL(mark_info_dirty);
  337. /*
  338. * Read dquot from disk and alloc space for it
  339. */
  340. int dquot_acquire(struct dquot *dquot)
  341. {
  342. int ret = 0, ret2 = 0;
  343. struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
  344. mutex_lock(&dquot->dq_lock);
  345. mutex_lock(&dqopt->dqio_mutex);
  346. if (!test_bit(DQ_READ_B, &dquot->dq_flags))
  347. ret = dqopt->ops[dquot->dq_type]->read_dqblk(dquot);
  348. if (ret < 0)
  349. goto out_iolock;
  350. set_bit(DQ_READ_B, &dquot->dq_flags);
  351. /* Instantiate dquot if needed */
  352. if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags) && !dquot->dq_off) {
  353. ret = dqopt->ops[dquot->dq_type]->commit_dqblk(dquot);
  354. /* Write the info if needed */
  355. if (info_dirty(&dqopt->info[dquot->dq_type])) {
  356. ret2 = dqopt->ops[dquot->dq_type]->write_file_info(
  357. dquot->dq_sb, dquot->dq_type);
  358. }
  359. if (ret < 0)
  360. goto out_iolock;
  361. if (ret2 < 0) {
  362. ret = ret2;
  363. goto out_iolock;
  364. }
  365. }
  366. set_bit(DQ_ACTIVE_B, &dquot->dq_flags);
  367. out_iolock:
  368. mutex_unlock(&dqopt->dqio_mutex);
  369. mutex_unlock(&dquot->dq_lock);
  370. return ret;
  371. }
  372. EXPORT_SYMBOL(dquot_acquire);
  373. /*
  374. * Write dquot to disk
  375. */
  376. int dquot_commit(struct dquot *dquot)
  377. {
  378. int ret = 0, ret2 = 0;
  379. struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
  380. mutex_lock(&dqopt->dqio_mutex);
  381. spin_lock(&dq_list_lock);
  382. if (!clear_dquot_dirty(dquot)) {
  383. spin_unlock(&dq_list_lock);
  384. goto out_sem;
  385. }
  386. spin_unlock(&dq_list_lock);
  387. /* Inactive dquot can be only if there was error during read/init
  388. * => we have better not writing it */
  389. if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
  390. ret = dqopt->ops[dquot->dq_type]->commit_dqblk(dquot);
  391. if (info_dirty(&dqopt->info[dquot->dq_type])) {
  392. ret2 = dqopt->ops[dquot->dq_type]->write_file_info(
  393. dquot->dq_sb, dquot->dq_type);
  394. }
  395. if (ret >= 0)
  396. ret = ret2;
  397. }
  398. out_sem:
  399. mutex_unlock(&dqopt->dqio_mutex);
  400. return ret;
  401. }
  402. EXPORT_SYMBOL(dquot_commit);
  403. /*
  404. * Release dquot
  405. */
  406. int dquot_release(struct dquot *dquot)
  407. {
  408. int ret = 0, ret2 = 0;
  409. struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
  410. mutex_lock(&dquot->dq_lock);
  411. /* Check whether we are not racing with some other dqget() */
  412. if (atomic_read(&dquot->dq_count) > 1)
  413. goto out_dqlock;
  414. mutex_lock(&dqopt->dqio_mutex);
  415. if (dqopt->ops[dquot->dq_type]->release_dqblk) {
  416. ret = dqopt->ops[dquot->dq_type]->release_dqblk(dquot);
  417. /* Write the info */
  418. if (info_dirty(&dqopt->info[dquot->dq_type])) {
  419. ret2 = dqopt->ops[dquot->dq_type]->write_file_info(
  420. dquot->dq_sb, dquot->dq_type);
  421. }
  422. if (ret >= 0)
  423. ret = ret2;
  424. }
  425. clear_bit(DQ_ACTIVE_B, &dquot->dq_flags);
  426. mutex_unlock(&dqopt->dqio_mutex);
  427. out_dqlock:
  428. mutex_unlock(&dquot->dq_lock);
  429. return ret;
  430. }
  431. EXPORT_SYMBOL(dquot_release);
  432. void dquot_destroy(struct dquot *dquot)
  433. {
  434. kmem_cache_free(dquot_cachep, dquot);
  435. }
  436. EXPORT_SYMBOL(dquot_destroy);
  437. static inline void do_destroy_dquot(struct dquot *dquot)
  438. {
  439. dquot->dq_sb->dq_op->destroy_dquot(dquot);
  440. }
  441. /* Invalidate all dquots on the list. Note that this function is called after
  442. * quota is disabled and pointers from inodes removed so there cannot be new
  443. * quota users. There can still be some users of quotas due to inodes being
  444. * just deleted or pruned by prune_icache() (those are not attached to any
  445. * list) or parallel quotactl call. We have to wait for such users.
  446. */
  447. static void invalidate_dquots(struct super_block *sb, int type)
  448. {
  449. struct dquot *dquot, *tmp;
  450. restart:
  451. spin_lock(&dq_list_lock);
  452. list_for_each_entry_safe(dquot, tmp, &inuse_list, dq_inuse) {
  453. if (dquot->dq_sb != sb)
  454. continue;
  455. if (dquot->dq_type != type)
  456. continue;
  457. /* Wait for dquot users */
  458. if (atomic_read(&dquot->dq_count)) {
  459. DEFINE_WAIT(wait);
  460. atomic_inc(&dquot->dq_count);
  461. prepare_to_wait(&dquot->dq_wait_unused, &wait,
  462. TASK_UNINTERRUPTIBLE);
  463. spin_unlock(&dq_list_lock);
  464. /* Once dqput() wakes us up, we know it's time to free
  465. * the dquot.
  466. * IMPORTANT: we rely on the fact that there is always
  467. * at most one process waiting for dquot to free.
  468. * Otherwise dq_count would be > 1 and we would never
  469. * wake up.
  470. */
  471. if (atomic_read(&dquot->dq_count) > 1)
  472. schedule();
  473. finish_wait(&dquot->dq_wait_unused, &wait);
  474. dqput(dquot);
  475. /* At this moment dquot() need not exist (it could be
  476. * reclaimed by prune_dqcache(). Hence we must
  477. * restart. */
  478. goto restart;
  479. }
  480. /*
  481. * Quota now has no users and it has been written on last
  482. * dqput()
  483. */
  484. remove_dquot_hash(dquot);
  485. remove_free_dquot(dquot);
  486. remove_inuse(dquot);
  487. do_destroy_dquot(dquot);
  488. }
  489. spin_unlock(&dq_list_lock);
  490. }
  491. /* Call callback for every active dquot on given filesystem */
  492. int dquot_scan_active(struct super_block *sb,
  493. int (*fn)(struct dquot *dquot, unsigned long priv),
  494. unsigned long priv)
  495. {
  496. struct dquot *dquot, *old_dquot = NULL;
  497. int ret = 0;
  498. mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
  499. spin_lock(&dq_list_lock);
  500. list_for_each_entry(dquot, &inuse_list, dq_inuse) {
  501. if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags))
  502. continue;
  503. if (dquot->dq_sb != sb)
  504. continue;
  505. /* Now we have active dquot so we can just increase use count */
  506. atomic_inc(&dquot->dq_count);
  507. dqstats.lookups++;
  508. spin_unlock(&dq_list_lock);
  509. dqput(old_dquot);
  510. old_dquot = dquot;
  511. ret = fn(dquot, priv);
  512. if (ret < 0)
  513. goto out;
  514. spin_lock(&dq_list_lock);
  515. /* We are safe to continue now because our dquot could not
  516. * be moved out of the inuse list while we hold the reference */
  517. }
  518. spin_unlock(&dq_list_lock);
  519. out:
  520. dqput(old_dquot);
  521. mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
  522. return ret;
  523. }
  524. EXPORT_SYMBOL(dquot_scan_active);
  525. int vfs_quota_sync(struct super_block *sb, int type, int wait)
  526. {
  527. struct list_head *dirty;
  528. struct dquot *dquot;
  529. struct quota_info *dqopt = sb_dqopt(sb);
  530. int cnt;
  531. mutex_lock(&dqopt->dqonoff_mutex);
  532. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  533. if (type != -1 && cnt != type)
  534. continue;
  535. if (!sb_has_quota_active(sb, cnt))
  536. continue;
  537. spin_lock(&dq_list_lock);
  538. dirty = &dqopt->info[cnt].dqi_dirty_list;
  539. while (!list_empty(dirty)) {
  540. dquot = list_first_entry(dirty, struct dquot,
  541. dq_dirty);
  542. /* Dirty and inactive can be only bad dquot... */
  543. if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
  544. clear_dquot_dirty(dquot);
  545. continue;
  546. }
  547. /* Now we have active dquot from which someone is
  548. * holding reference so we can safely just increase
  549. * use count */
  550. atomic_inc(&dquot->dq_count);
  551. dqstats.lookups++;
  552. spin_unlock(&dq_list_lock);
  553. sb->dq_op->write_dquot(dquot);
  554. dqput(dquot);
  555. spin_lock(&dq_list_lock);
  556. }
  557. spin_unlock(&dq_list_lock);
  558. }
  559. for (cnt = 0; cnt < MAXQUOTAS; cnt++)
  560. if ((cnt == type || type == -1) && sb_has_quota_active(sb, cnt)
  561. && info_dirty(&dqopt->info[cnt]))
  562. sb->dq_op->write_info(sb, cnt);
  563. spin_lock(&dq_list_lock);
  564. dqstats.syncs++;
  565. spin_unlock(&dq_list_lock);
  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(vfs_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(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 (dqstats.free_dquots / 100) * sysctl_vfs_cache_pressure;
  622. }
  623. static struct shrinker dqcache_shrinker = {
  624. .shrink = shrink_dqcache_memory,
  625. .seeks = DEFAULT_SEEKS,
  626. };
  627. /*
  628. * Put reference to dquot
  629. * NOTE: If you change this function please check whether dqput_blocks() works right...
  630. */
  631. void dqput(struct dquot *dquot)
  632. {
  633. int ret;
  634. if (!dquot)
  635. return;
  636. #ifdef CONFIG_QUOTA_DEBUG
  637. if (!atomic_read(&dquot->dq_count)) {
  638. printk("VFS: dqput: trying to free free dquot\n");
  639. printk("VFS: device %s, dquot of %s %d\n",
  640. dquot->dq_sb->s_id,
  641. quotatypes[dquot->dq_type],
  642. dquot->dq_id);
  643. BUG();
  644. }
  645. #endif
  646. spin_lock(&dq_list_lock);
  647. dqstats.drops++;
  648. spin_unlock(&dq_list_lock);
  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. dqstats.lookups++;
  758. spin_unlock(&dq_list_lock);
  759. } else {
  760. if (!atomic_read(&dquot->dq_count))
  761. remove_free_dquot(dquot);
  762. atomic_inc(&dquot->dq_count);
  763. dqstats.cache_hits++;
  764. dqstats.lookups++;
  765. spin_unlock(&dq_list_lock);
  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,
  1348. int warn, int reserve)
  1349. {
  1350. int cnt, ret = 0;
  1351. char warntype[MAXQUOTAS];
  1352. /*
  1353. * First test before acquiring mutex - solves deadlocks when we
  1354. * re-enter the quota code and are already holding the mutex
  1355. */
  1356. if (!sb_any_quota_active(inode->i_sb) || IS_NOQUOTA(inode)) {
  1357. inode_incr_space(inode, number, reserve);
  1358. goto out;
  1359. }
  1360. down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1361. for (cnt = 0; cnt < MAXQUOTAS; cnt++)
  1362. warntype[cnt] = QUOTA_NL_NOWARN;
  1363. spin_lock(&dq_data_lock);
  1364. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1365. if (!inode->i_dquot[cnt])
  1366. continue;
  1367. ret = check_bdq(inode->i_dquot[cnt], number, !warn,
  1368. warntype+cnt);
  1369. if (ret) {
  1370. spin_unlock(&dq_data_lock);
  1371. goto out_flush_warn;
  1372. }
  1373. }
  1374. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1375. if (!inode->i_dquot[cnt])
  1376. continue;
  1377. if (reserve)
  1378. dquot_resv_space(inode->i_dquot[cnt], number);
  1379. else
  1380. dquot_incr_space(inode->i_dquot[cnt], number);
  1381. }
  1382. inode_incr_space(inode, number, reserve);
  1383. spin_unlock(&dq_data_lock);
  1384. if (reserve)
  1385. goto out_flush_warn;
  1386. mark_all_dquot_dirty(inode->i_dquot);
  1387. out_flush_warn:
  1388. flush_warnings(inode->i_dquot, warntype);
  1389. up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1390. out:
  1391. return ret;
  1392. }
  1393. EXPORT_SYMBOL(__dquot_alloc_space);
  1394. /*
  1395. * This operation can block, but only after everything is updated
  1396. */
  1397. int dquot_alloc_inode(const struct inode *inode)
  1398. {
  1399. int cnt, ret = 0;
  1400. char warntype[MAXQUOTAS];
  1401. /* First test before acquiring mutex - solves deadlocks when we
  1402. * re-enter the quota code and are already holding the mutex */
  1403. if (!sb_any_quota_active(inode->i_sb) || IS_NOQUOTA(inode))
  1404. return 0;
  1405. for (cnt = 0; cnt < MAXQUOTAS; cnt++)
  1406. warntype[cnt] = QUOTA_NL_NOWARN;
  1407. down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1408. spin_lock(&dq_data_lock);
  1409. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1410. if (!inode->i_dquot[cnt])
  1411. continue;
  1412. ret = check_idq(inode->i_dquot[cnt], 1, warntype + cnt);
  1413. if (ret)
  1414. goto warn_put_all;
  1415. }
  1416. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1417. if (!inode->i_dquot[cnt])
  1418. continue;
  1419. dquot_incr_inodes(inode->i_dquot[cnt], 1);
  1420. }
  1421. warn_put_all:
  1422. spin_unlock(&dq_data_lock);
  1423. if (ret == 0)
  1424. mark_all_dquot_dirty(inode->i_dquot);
  1425. flush_warnings(inode->i_dquot, warntype);
  1426. up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1427. return ret;
  1428. }
  1429. EXPORT_SYMBOL(dquot_alloc_inode);
  1430. /*
  1431. * Convert in-memory reserved quotas to real consumed quotas
  1432. */
  1433. int dquot_claim_space_nodirty(struct inode *inode, qsize_t number)
  1434. {
  1435. int cnt;
  1436. if (!sb_any_quota_active(inode->i_sb) || IS_NOQUOTA(inode)) {
  1437. inode_claim_rsv_space(inode, number);
  1438. return 0;
  1439. }
  1440. down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1441. spin_lock(&dq_data_lock);
  1442. /* Claim reserved quotas to allocated quotas */
  1443. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1444. if (inode->i_dquot[cnt])
  1445. dquot_claim_reserved_space(inode->i_dquot[cnt],
  1446. number);
  1447. }
  1448. /* Update inode bytes */
  1449. inode_claim_rsv_space(inode, number);
  1450. spin_unlock(&dq_data_lock);
  1451. mark_all_dquot_dirty(inode->i_dquot);
  1452. up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1453. return 0;
  1454. }
  1455. EXPORT_SYMBOL(dquot_claim_space_nodirty);
  1456. /*
  1457. * This operation can block, but only after everything is updated
  1458. */
  1459. void __dquot_free_space(struct inode *inode, qsize_t number, int reserve)
  1460. {
  1461. unsigned int cnt;
  1462. char warntype[MAXQUOTAS];
  1463. /* First test before acquiring mutex - solves deadlocks when we
  1464. * re-enter the quota code and are already holding the mutex */
  1465. if (!sb_any_quota_active(inode->i_sb) || IS_NOQUOTA(inode)) {
  1466. inode_decr_space(inode, number, reserve);
  1467. return;
  1468. }
  1469. down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1470. spin_lock(&dq_data_lock);
  1471. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1472. if (!inode->i_dquot[cnt])
  1473. continue;
  1474. warntype[cnt] = info_bdq_free(inode->i_dquot[cnt], number);
  1475. if (reserve)
  1476. dquot_free_reserved_space(inode->i_dquot[cnt], number);
  1477. else
  1478. dquot_decr_space(inode->i_dquot[cnt], number);
  1479. }
  1480. inode_decr_space(inode, number, reserve);
  1481. spin_unlock(&dq_data_lock);
  1482. if (reserve)
  1483. goto out_unlock;
  1484. mark_all_dquot_dirty(inode->i_dquot);
  1485. out_unlock:
  1486. flush_warnings(inode->i_dquot, warntype);
  1487. up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1488. }
  1489. EXPORT_SYMBOL(__dquot_free_space);
  1490. /*
  1491. * This operation can block, but only after everything is updated
  1492. */
  1493. void dquot_free_inode(const struct inode *inode)
  1494. {
  1495. unsigned int cnt;
  1496. char warntype[MAXQUOTAS];
  1497. /* First test before acquiring mutex - solves deadlocks when we
  1498. * re-enter the quota code and are already holding the mutex */
  1499. if (!sb_any_quota_active(inode->i_sb) || IS_NOQUOTA(inode))
  1500. return;
  1501. down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1502. spin_lock(&dq_data_lock);
  1503. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1504. if (!inode->i_dquot[cnt])
  1505. continue;
  1506. warntype[cnt] = info_idq_free(inode->i_dquot[cnt], 1);
  1507. dquot_decr_inodes(inode->i_dquot[cnt], 1);
  1508. }
  1509. spin_unlock(&dq_data_lock);
  1510. mark_all_dquot_dirty(inode->i_dquot);
  1511. flush_warnings(inode->i_dquot, warntype);
  1512. up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1513. }
  1514. EXPORT_SYMBOL(dquot_free_inode);
  1515. /*
  1516. * Transfer the number of inode and blocks from one diskquota to an other.
  1517. *
  1518. * This operation can block, but only after everything is updated
  1519. * A transaction must be started when entering this function.
  1520. */
  1521. static int __dquot_transfer(struct inode *inode, qid_t *chid, unsigned long mask)
  1522. {
  1523. qsize_t space, cur_space;
  1524. qsize_t rsv_space = 0;
  1525. struct dquot *transfer_from[MAXQUOTAS];
  1526. struct dquot *transfer_to[MAXQUOTAS];
  1527. int cnt, ret = 0;
  1528. char warntype_to[MAXQUOTAS];
  1529. char warntype_from_inodes[MAXQUOTAS], warntype_from_space[MAXQUOTAS];
  1530. /* First test before acquiring mutex - solves deadlocks when we
  1531. * re-enter the quota code and are already holding the mutex */
  1532. if (IS_NOQUOTA(inode))
  1533. return 0;
  1534. /* Initialize the arrays */
  1535. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1536. transfer_from[cnt] = NULL;
  1537. transfer_to[cnt] = NULL;
  1538. warntype_to[cnt] = QUOTA_NL_NOWARN;
  1539. }
  1540. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1541. if (mask & (1 << cnt))
  1542. transfer_to[cnt] = dqget(inode->i_sb, chid[cnt], cnt);
  1543. }
  1544. down_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1545. if (IS_NOQUOTA(inode)) { /* File without quota accounting? */
  1546. up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1547. goto put_all;
  1548. }
  1549. spin_lock(&dq_data_lock);
  1550. cur_space = inode_get_bytes(inode);
  1551. rsv_space = inode_get_rsv_space(inode);
  1552. space = cur_space + rsv_space;
  1553. /* Build the transfer_from list and check the limits */
  1554. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1555. if (!transfer_to[cnt])
  1556. continue;
  1557. transfer_from[cnt] = inode->i_dquot[cnt];
  1558. ret = check_idq(transfer_to[cnt], 1, warntype_to + cnt);
  1559. if (ret)
  1560. goto over_quota;
  1561. ret = check_bdq(transfer_to[cnt], space, 0, warntype_to + cnt);
  1562. if (ret)
  1563. goto over_quota;
  1564. }
  1565. /*
  1566. * Finally perform the needed transfer from transfer_from to transfer_to
  1567. */
  1568. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1569. /*
  1570. * Skip changes for same uid or gid or for turned off quota-type.
  1571. */
  1572. if (!transfer_to[cnt])
  1573. continue;
  1574. /* Due to IO error we might not have transfer_from[] structure */
  1575. if (transfer_from[cnt]) {
  1576. warntype_from_inodes[cnt] =
  1577. info_idq_free(transfer_from[cnt], 1);
  1578. warntype_from_space[cnt] =
  1579. info_bdq_free(transfer_from[cnt], space);
  1580. dquot_decr_inodes(transfer_from[cnt], 1);
  1581. dquot_decr_space(transfer_from[cnt], cur_space);
  1582. dquot_free_reserved_space(transfer_from[cnt],
  1583. rsv_space);
  1584. }
  1585. dquot_incr_inodes(transfer_to[cnt], 1);
  1586. dquot_incr_space(transfer_to[cnt], cur_space);
  1587. dquot_resv_space(transfer_to[cnt], rsv_space);
  1588. inode->i_dquot[cnt] = transfer_to[cnt];
  1589. }
  1590. spin_unlock(&dq_data_lock);
  1591. up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1592. mark_all_dquot_dirty(transfer_from);
  1593. mark_all_dquot_dirty(transfer_to);
  1594. /* The reference we got is transferred to the inode */
  1595. for (cnt = 0; cnt < MAXQUOTAS; cnt++)
  1596. transfer_to[cnt] = NULL;
  1597. warn_put_all:
  1598. flush_warnings(transfer_to, warntype_to);
  1599. flush_warnings(transfer_from, warntype_from_inodes);
  1600. flush_warnings(transfer_from, warntype_from_space);
  1601. put_all:
  1602. dqput_all(transfer_from);
  1603. dqput_all(transfer_to);
  1604. return ret;
  1605. over_quota:
  1606. spin_unlock(&dq_data_lock);
  1607. up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1608. /* Clear dquot pointers we don't want to dqput() */
  1609. for (cnt = 0; cnt < MAXQUOTAS; cnt++)
  1610. transfer_from[cnt] = NULL;
  1611. goto warn_put_all;
  1612. }
  1613. /* Wrapper for transferring ownership of an inode for uid/gid only
  1614. * Called from FSXXX_setattr()
  1615. */
  1616. int dquot_transfer(struct inode *inode, struct iattr *iattr)
  1617. {
  1618. qid_t chid[MAXQUOTAS];
  1619. unsigned long mask = 0;
  1620. if (iattr->ia_valid & ATTR_UID && iattr->ia_uid != inode->i_uid) {
  1621. mask |= 1 << USRQUOTA;
  1622. chid[USRQUOTA] = iattr->ia_uid;
  1623. }
  1624. if (iattr->ia_valid & ATTR_GID && iattr->ia_gid != inode->i_gid) {
  1625. mask |= 1 << GRPQUOTA;
  1626. chid[GRPQUOTA] = iattr->ia_gid;
  1627. }
  1628. if (sb_any_quota_active(inode->i_sb) && !IS_NOQUOTA(inode)) {
  1629. dquot_initialize(inode);
  1630. return __dquot_transfer(inode, chid, mask);
  1631. }
  1632. return 0;
  1633. }
  1634. EXPORT_SYMBOL(dquot_transfer);
  1635. /*
  1636. * Write info of quota file to disk
  1637. */
  1638. int dquot_commit_info(struct super_block *sb, int type)
  1639. {
  1640. int ret;
  1641. struct quota_info *dqopt = sb_dqopt(sb);
  1642. mutex_lock(&dqopt->dqio_mutex);
  1643. ret = dqopt->ops[type]->write_file_info(sb, type);
  1644. mutex_unlock(&dqopt->dqio_mutex);
  1645. return ret;
  1646. }
  1647. EXPORT_SYMBOL(dquot_commit_info);
  1648. /*
  1649. * Definitions of diskquota operations.
  1650. */
  1651. const struct dquot_operations dquot_operations = {
  1652. .write_dquot = dquot_commit,
  1653. .acquire_dquot = dquot_acquire,
  1654. .release_dquot = dquot_release,
  1655. .mark_dirty = dquot_mark_dquot_dirty,
  1656. .write_info = dquot_commit_info,
  1657. .alloc_dquot = dquot_alloc,
  1658. .destroy_dquot = dquot_destroy,
  1659. };
  1660. /*
  1661. * Generic helper for ->open on filesystems supporting disk quotas.
  1662. */
  1663. int dquot_file_open(struct inode *inode, struct file *file)
  1664. {
  1665. int error;
  1666. error = generic_file_open(inode, file);
  1667. if (!error && (file->f_mode & FMODE_WRITE))
  1668. dquot_initialize(inode);
  1669. return error;
  1670. }
  1671. EXPORT_SYMBOL(dquot_file_open);
  1672. /*
  1673. * Turn quota off on a device. type == -1 ==> quotaoff for all types (umount)
  1674. */
  1675. int vfs_quota_disable(struct super_block *sb, int type, unsigned int flags)
  1676. {
  1677. int cnt, ret = 0;
  1678. struct quota_info *dqopt = sb_dqopt(sb);
  1679. struct inode *toputinode[MAXQUOTAS];
  1680. /* Cannot turn off usage accounting without turning off limits, or
  1681. * suspend quotas and simultaneously turn quotas off. */
  1682. if ((flags & DQUOT_USAGE_ENABLED && !(flags & DQUOT_LIMITS_ENABLED))
  1683. || (flags & DQUOT_SUSPENDED && flags & (DQUOT_LIMITS_ENABLED |
  1684. DQUOT_USAGE_ENABLED)))
  1685. return -EINVAL;
  1686. /* We need to serialize quota_off() for device */
  1687. mutex_lock(&dqopt->dqonoff_mutex);
  1688. /*
  1689. * Skip everything if there's nothing to do. We have to do this because
  1690. * sometimes we are called when fill_super() failed and calling
  1691. * sync_fs() in such cases does no good.
  1692. */
  1693. if (!sb_any_quota_loaded(sb)) {
  1694. mutex_unlock(&dqopt->dqonoff_mutex);
  1695. return 0;
  1696. }
  1697. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1698. toputinode[cnt] = NULL;
  1699. if (type != -1 && cnt != type)
  1700. continue;
  1701. if (!sb_has_quota_loaded(sb, cnt))
  1702. continue;
  1703. if (flags & DQUOT_SUSPENDED) {
  1704. spin_lock(&dq_state_lock);
  1705. dqopt->flags |=
  1706. dquot_state_flag(DQUOT_SUSPENDED, cnt);
  1707. spin_unlock(&dq_state_lock);
  1708. } else {
  1709. spin_lock(&dq_state_lock);
  1710. dqopt->flags &= ~dquot_state_flag(flags, cnt);
  1711. /* Turning off suspended quotas? */
  1712. if (!sb_has_quota_loaded(sb, cnt) &&
  1713. sb_has_quota_suspended(sb, cnt)) {
  1714. dqopt->flags &= ~dquot_state_flag(
  1715. DQUOT_SUSPENDED, cnt);
  1716. spin_unlock(&dq_state_lock);
  1717. iput(dqopt->files[cnt]);
  1718. dqopt->files[cnt] = NULL;
  1719. continue;
  1720. }
  1721. spin_unlock(&dq_state_lock);
  1722. }
  1723. /* We still have to keep quota loaded? */
  1724. if (sb_has_quota_loaded(sb, cnt) && !(flags & DQUOT_SUSPENDED))
  1725. continue;
  1726. /* Note: these are blocking operations */
  1727. drop_dquot_ref(sb, cnt);
  1728. invalidate_dquots(sb, cnt);
  1729. /*
  1730. * Now all dquots should be invalidated, all writes done so we
  1731. * should be only users of the info. No locks needed.
  1732. */
  1733. if (info_dirty(&dqopt->info[cnt]))
  1734. sb->dq_op->write_info(sb, cnt);
  1735. if (dqopt->ops[cnt]->free_file_info)
  1736. dqopt->ops[cnt]->free_file_info(sb, cnt);
  1737. put_quota_format(dqopt->info[cnt].dqi_format);
  1738. toputinode[cnt] = dqopt->files[cnt];
  1739. if (!sb_has_quota_loaded(sb, cnt))
  1740. dqopt->files[cnt] = NULL;
  1741. dqopt->info[cnt].dqi_flags = 0;
  1742. dqopt->info[cnt].dqi_igrace = 0;
  1743. dqopt->info[cnt].dqi_bgrace = 0;
  1744. dqopt->ops[cnt] = NULL;
  1745. }
  1746. mutex_unlock(&dqopt->dqonoff_mutex);
  1747. /* Skip syncing and setting flags if quota files are hidden */
  1748. if (dqopt->flags & DQUOT_QUOTA_SYS_FILE)
  1749. goto put_inodes;
  1750. /* Sync the superblock so that buffers with quota data are written to
  1751. * disk (and so userspace sees correct data afterwards). */
  1752. if (sb->s_op->sync_fs)
  1753. sb->s_op->sync_fs(sb, 1);
  1754. sync_blockdev(sb->s_bdev);
  1755. /* Now the quota files are just ordinary files and we can set the
  1756. * inode flags back. Moreover we discard the pagecache so that
  1757. * userspace sees the writes we did bypassing the pagecache. We
  1758. * must also discard the blockdev buffers so that we see the
  1759. * changes done by userspace on the next quotaon() */
  1760. for (cnt = 0; cnt < MAXQUOTAS; cnt++)
  1761. if (toputinode[cnt]) {
  1762. mutex_lock(&dqopt->dqonoff_mutex);
  1763. /* If quota was reenabled in the meantime, we have
  1764. * nothing to do */
  1765. if (!sb_has_quota_loaded(sb, cnt)) {
  1766. mutex_lock_nested(&toputinode[cnt]->i_mutex,
  1767. I_MUTEX_QUOTA);
  1768. toputinode[cnt]->i_flags &= ~(S_IMMUTABLE |
  1769. S_NOATIME | S_NOQUOTA);
  1770. truncate_inode_pages(&toputinode[cnt]->i_data,
  1771. 0);
  1772. mutex_unlock(&toputinode[cnt]->i_mutex);
  1773. mark_inode_dirty(toputinode[cnt]);
  1774. }
  1775. mutex_unlock(&dqopt->dqonoff_mutex);
  1776. }
  1777. if (sb->s_bdev)
  1778. invalidate_bdev(sb->s_bdev);
  1779. put_inodes:
  1780. for (cnt = 0; cnt < MAXQUOTAS; cnt++)
  1781. if (toputinode[cnt]) {
  1782. /* On remount RO, we keep the inode pointer so that we
  1783. * can reenable quota on the subsequent remount RW. We
  1784. * have to check 'flags' variable and not use sb_has_
  1785. * function because another quotaon / quotaoff could
  1786. * change global state before we got here. We refuse
  1787. * to suspend quotas when there is pending delete on
  1788. * the quota file... */
  1789. if (!(flags & DQUOT_SUSPENDED))
  1790. iput(toputinode[cnt]);
  1791. else if (!toputinode[cnt]->i_nlink)
  1792. ret = -EBUSY;
  1793. }
  1794. return ret;
  1795. }
  1796. EXPORT_SYMBOL(vfs_quota_disable);
  1797. int vfs_quota_off(struct super_block *sb, int type, int remount)
  1798. {
  1799. return vfs_quota_disable(sb, type, remount ? DQUOT_SUSPENDED :
  1800. (DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED));
  1801. }
  1802. EXPORT_SYMBOL(vfs_quota_off);
  1803. /*
  1804. * Turn quotas on on a device
  1805. */
  1806. /*
  1807. * Helper function to turn quotas on when we already have the inode of
  1808. * quota file and no quota information is loaded.
  1809. */
  1810. static int vfs_load_quota_inode(struct inode *inode, int type, int format_id,
  1811. unsigned int flags)
  1812. {
  1813. struct quota_format_type *fmt = find_quota_format(format_id);
  1814. struct super_block *sb = inode->i_sb;
  1815. struct quota_info *dqopt = sb_dqopt(sb);
  1816. int error;
  1817. int oldflags = -1;
  1818. if (!fmt)
  1819. return -ESRCH;
  1820. if (!S_ISREG(inode->i_mode)) {
  1821. error = -EACCES;
  1822. goto out_fmt;
  1823. }
  1824. if (IS_RDONLY(inode)) {
  1825. error = -EROFS;
  1826. goto out_fmt;
  1827. }
  1828. if (!sb->s_op->quota_write || !sb->s_op->quota_read) {
  1829. error = -EINVAL;
  1830. goto out_fmt;
  1831. }
  1832. /* Usage always has to be set... */
  1833. if (!(flags & DQUOT_USAGE_ENABLED)) {
  1834. error = -EINVAL;
  1835. goto out_fmt;
  1836. }
  1837. if (!(dqopt->flags & DQUOT_QUOTA_SYS_FILE)) {
  1838. /* As we bypass the pagecache we must now flush all the
  1839. * dirty data and invalidate caches so that kernel sees
  1840. * changes from userspace. It is not enough to just flush
  1841. * the quota file since if blocksize < pagesize, invalidation
  1842. * of the cache could fail because of other unrelated dirty
  1843. * data */
  1844. sync_filesystem(sb);
  1845. invalidate_bdev(sb->s_bdev);
  1846. }
  1847. mutex_lock(&dqopt->dqonoff_mutex);
  1848. if (sb_has_quota_loaded(sb, type)) {
  1849. error = -EBUSY;
  1850. goto out_lock;
  1851. }
  1852. if (!(dqopt->flags & DQUOT_QUOTA_SYS_FILE)) {
  1853. /* We don't want quota and atime on quota files (deadlocks
  1854. * possible) Also nobody should write to the file - we use
  1855. * special IO operations which ignore the immutable bit. */
  1856. mutex_lock_nested(&inode->i_mutex, I_MUTEX_QUOTA);
  1857. oldflags = inode->i_flags & (S_NOATIME | S_IMMUTABLE |
  1858. S_NOQUOTA);
  1859. inode->i_flags |= S_NOQUOTA | S_NOATIME | S_IMMUTABLE;
  1860. mutex_unlock(&inode->i_mutex);
  1861. /*
  1862. * When S_NOQUOTA is set, remove dquot references as no more
  1863. * references can be added
  1864. */
  1865. __dquot_drop(inode);
  1866. }
  1867. error = -EIO;
  1868. dqopt->files[type] = igrab(inode);
  1869. if (!dqopt->files[type])
  1870. goto out_lock;
  1871. error = -EINVAL;
  1872. if (!fmt->qf_ops->check_quota_file(sb, type))
  1873. goto out_file_init;
  1874. dqopt->ops[type] = fmt->qf_ops;
  1875. dqopt->info[type].dqi_format = fmt;
  1876. dqopt->info[type].dqi_fmt_id = format_id;
  1877. INIT_LIST_HEAD(&dqopt->info[type].dqi_dirty_list);
  1878. mutex_lock(&dqopt->dqio_mutex);
  1879. error = dqopt->ops[type]->read_file_info(sb, type);
  1880. if (error < 0) {
  1881. mutex_unlock(&dqopt->dqio_mutex);
  1882. goto out_file_init;
  1883. }
  1884. mutex_unlock(&dqopt->dqio_mutex);
  1885. spin_lock(&dq_state_lock);
  1886. dqopt->flags |= dquot_state_flag(flags, type);
  1887. spin_unlock(&dq_state_lock);
  1888. add_dquot_ref(sb, type);
  1889. mutex_unlock(&dqopt->dqonoff_mutex);
  1890. return 0;
  1891. out_file_init:
  1892. dqopt->files[type] = NULL;
  1893. iput(inode);
  1894. out_lock:
  1895. if (oldflags != -1) {
  1896. mutex_lock_nested(&inode->i_mutex, I_MUTEX_QUOTA);
  1897. /* Set the flags back (in the case of accidental quotaon()
  1898. * on a wrong file we don't want to mess up the flags) */
  1899. inode->i_flags &= ~(S_NOATIME | S_NOQUOTA | S_IMMUTABLE);
  1900. inode->i_flags |= oldflags;
  1901. mutex_unlock(&inode->i_mutex);
  1902. }
  1903. mutex_unlock(&dqopt->dqonoff_mutex);
  1904. out_fmt:
  1905. put_quota_format(fmt);
  1906. return error;
  1907. }
  1908. /* Reenable quotas on remount RW */
  1909. static int vfs_quota_on_remount(struct super_block *sb, int type)
  1910. {
  1911. struct quota_info *dqopt = sb_dqopt(sb);
  1912. struct inode *inode;
  1913. int ret;
  1914. unsigned int flags;
  1915. mutex_lock(&dqopt->dqonoff_mutex);
  1916. if (!sb_has_quota_suspended(sb, type)) {
  1917. mutex_unlock(&dqopt->dqonoff_mutex);
  1918. return 0;
  1919. }
  1920. inode = dqopt->files[type];
  1921. dqopt->files[type] = NULL;
  1922. spin_lock(&dq_state_lock);
  1923. flags = dqopt->flags & dquot_state_flag(DQUOT_USAGE_ENABLED |
  1924. DQUOT_LIMITS_ENABLED, type);
  1925. dqopt->flags &= ~dquot_state_flag(DQUOT_STATE_FLAGS, type);
  1926. spin_unlock(&dq_state_lock);
  1927. mutex_unlock(&dqopt->dqonoff_mutex);
  1928. flags = dquot_generic_flag(flags, type);
  1929. ret = vfs_load_quota_inode(inode, type, dqopt->info[type].dqi_fmt_id,
  1930. flags);
  1931. iput(inode);
  1932. return ret;
  1933. }
  1934. int vfs_quota_on_path(struct super_block *sb, int type, int format_id,
  1935. struct path *path)
  1936. {
  1937. int error = security_quota_on(path->dentry);
  1938. if (error)
  1939. return error;
  1940. /* Quota file not on the same filesystem? */
  1941. if (path->mnt->mnt_sb != sb)
  1942. error = -EXDEV;
  1943. else
  1944. error = vfs_load_quota_inode(path->dentry->d_inode, type,
  1945. format_id, DQUOT_USAGE_ENABLED |
  1946. DQUOT_LIMITS_ENABLED);
  1947. return error;
  1948. }
  1949. EXPORT_SYMBOL(vfs_quota_on_path);
  1950. int vfs_quota_on(struct super_block *sb, int type, int format_id, char *name,
  1951. int remount)
  1952. {
  1953. struct path path;
  1954. int error;
  1955. if (remount)
  1956. return vfs_quota_on_remount(sb, type);
  1957. error = kern_path(name, LOOKUP_FOLLOW, &path);
  1958. if (!error) {
  1959. error = vfs_quota_on_path(sb, type, format_id, &path);
  1960. path_put(&path);
  1961. }
  1962. return error;
  1963. }
  1964. EXPORT_SYMBOL(vfs_quota_on);
  1965. /*
  1966. * More powerful function for turning on quotas allowing setting
  1967. * of individual quota flags
  1968. */
  1969. int vfs_quota_enable(struct inode *inode, int type, int format_id,
  1970. unsigned int flags)
  1971. {
  1972. int ret = 0;
  1973. struct super_block *sb = inode->i_sb;
  1974. struct quota_info *dqopt = sb_dqopt(sb);
  1975. /* Just unsuspend quotas? */
  1976. if (flags & DQUOT_SUSPENDED)
  1977. return vfs_quota_on_remount(sb, type);
  1978. if (!flags)
  1979. return 0;
  1980. /* Just updating flags needed? */
  1981. if (sb_has_quota_loaded(sb, type)) {
  1982. mutex_lock(&dqopt->dqonoff_mutex);
  1983. /* Now do a reliable test... */
  1984. if (!sb_has_quota_loaded(sb, type)) {
  1985. mutex_unlock(&dqopt->dqonoff_mutex);
  1986. goto load_quota;
  1987. }
  1988. if (flags & DQUOT_USAGE_ENABLED &&
  1989. sb_has_quota_usage_enabled(sb, type)) {
  1990. ret = -EBUSY;
  1991. goto out_lock;
  1992. }
  1993. if (flags & DQUOT_LIMITS_ENABLED &&
  1994. sb_has_quota_limits_enabled(sb, type)) {
  1995. ret = -EBUSY;
  1996. goto out_lock;
  1997. }
  1998. spin_lock(&dq_state_lock);
  1999. sb_dqopt(sb)->flags |= dquot_state_flag(flags, type);
  2000. spin_unlock(&dq_state_lock);
  2001. out_lock:
  2002. mutex_unlock(&dqopt->dqonoff_mutex);
  2003. return ret;
  2004. }
  2005. load_quota:
  2006. return vfs_load_quota_inode(inode, type, format_id, flags);
  2007. }
  2008. EXPORT_SYMBOL(vfs_quota_enable);
  2009. /*
  2010. * This function is used when filesystem needs to initialize quotas
  2011. * during mount time.
  2012. */
  2013. int vfs_quota_on_mount(struct super_block *sb, char *qf_name,
  2014. int format_id, int type)
  2015. {
  2016. struct dentry *dentry;
  2017. int error;
  2018. mutex_lock(&sb->s_root->d_inode->i_mutex);
  2019. dentry = lookup_one_len(qf_name, sb->s_root, strlen(qf_name));
  2020. mutex_unlock(&sb->s_root->d_inode->i_mutex);
  2021. if (IS_ERR(dentry))
  2022. return PTR_ERR(dentry);
  2023. if (!dentry->d_inode) {
  2024. error = -ENOENT;
  2025. goto out;
  2026. }
  2027. error = security_quota_on(dentry);
  2028. if (!error)
  2029. error = vfs_load_quota_inode(dentry->d_inode, type, format_id,
  2030. DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED);
  2031. out:
  2032. dput(dentry);
  2033. return error;
  2034. }
  2035. EXPORT_SYMBOL(vfs_quota_on_mount);
  2036. /* Wrapper to turn on quotas when remounting rw */
  2037. int vfs_dq_quota_on_remount(struct super_block *sb)
  2038. {
  2039. int cnt;
  2040. int ret = 0, err;
  2041. if (!sb->s_qcop || !sb->s_qcop->quota_on)
  2042. return -ENOSYS;
  2043. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  2044. err = sb->s_qcop->quota_on(sb, cnt, 0, NULL, 1);
  2045. if (err < 0 && !ret)
  2046. ret = err;
  2047. }
  2048. return ret;
  2049. }
  2050. EXPORT_SYMBOL(vfs_dq_quota_on_remount);
  2051. static inline qsize_t qbtos(qsize_t blocks)
  2052. {
  2053. return blocks << QIF_DQBLKSIZE_BITS;
  2054. }
  2055. static inline qsize_t stoqb(qsize_t space)
  2056. {
  2057. return (space + QIF_DQBLKSIZE - 1) >> QIF_DQBLKSIZE_BITS;
  2058. }
  2059. /* Generic routine for getting common part of quota structure */
  2060. static void do_get_dqblk(struct dquot *dquot, struct if_dqblk *di)
  2061. {
  2062. struct mem_dqblk *dm = &dquot->dq_dqb;
  2063. spin_lock(&dq_data_lock);
  2064. di->dqb_bhardlimit = stoqb(dm->dqb_bhardlimit);
  2065. di->dqb_bsoftlimit = stoqb(dm->dqb_bsoftlimit);
  2066. di->dqb_curspace = dm->dqb_curspace + dm->dqb_rsvspace;
  2067. di->dqb_ihardlimit = dm->dqb_ihardlimit;
  2068. di->dqb_isoftlimit = dm->dqb_isoftlimit;
  2069. di->dqb_curinodes = dm->dqb_curinodes;
  2070. di->dqb_btime = dm->dqb_btime;
  2071. di->dqb_itime = dm->dqb_itime;
  2072. di->dqb_valid = QIF_ALL;
  2073. spin_unlock(&dq_data_lock);
  2074. }
  2075. int vfs_get_dqblk(struct super_block *sb, int type, qid_t id,
  2076. struct if_dqblk *di)
  2077. {
  2078. struct dquot *dquot;
  2079. dquot = dqget(sb, id, type);
  2080. if (!dquot)
  2081. return -ESRCH;
  2082. do_get_dqblk(dquot, di);
  2083. dqput(dquot);
  2084. return 0;
  2085. }
  2086. EXPORT_SYMBOL(vfs_get_dqblk);
  2087. /* Generic routine for setting common part of quota structure */
  2088. static int do_set_dqblk(struct dquot *dquot, struct if_dqblk *di)
  2089. {
  2090. struct mem_dqblk *dm = &dquot->dq_dqb;
  2091. int check_blim = 0, check_ilim = 0;
  2092. struct mem_dqinfo *dqi = &sb_dqopt(dquot->dq_sb)->info[dquot->dq_type];
  2093. if ((di->dqb_valid & QIF_BLIMITS &&
  2094. (di->dqb_bhardlimit > dqi->dqi_maxblimit ||
  2095. di->dqb_bsoftlimit > dqi->dqi_maxblimit)) ||
  2096. (di->dqb_valid & QIF_ILIMITS &&
  2097. (di->dqb_ihardlimit > dqi->dqi_maxilimit ||
  2098. di->dqb_isoftlimit > dqi->dqi_maxilimit)))
  2099. return -ERANGE;
  2100. spin_lock(&dq_data_lock);
  2101. if (di->dqb_valid & QIF_SPACE) {
  2102. dm->dqb_curspace = di->dqb_curspace - dm->dqb_rsvspace;
  2103. check_blim = 1;
  2104. set_bit(DQ_LASTSET_B + QIF_SPACE_B, &dquot->dq_flags);
  2105. }
  2106. if (di->dqb_valid & QIF_BLIMITS) {
  2107. dm->dqb_bsoftlimit = qbtos(di->dqb_bsoftlimit);
  2108. dm->dqb_bhardlimit = qbtos(di->dqb_bhardlimit);
  2109. check_blim = 1;
  2110. set_bit(DQ_LASTSET_B + QIF_BLIMITS_B, &dquot->dq_flags);
  2111. }
  2112. if (di->dqb_valid & QIF_INODES) {
  2113. dm->dqb_curinodes = di->dqb_curinodes;
  2114. check_ilim = 1;
  2115. set_bit(DQ_LASTSET_B + QIF_INODES_B, &dquot->dq_flags);
  2116. }
  2117. if (di->dqb_valid & QIF_ILIMITS) {
  2118. dm->dqb_isoftlimit = di->dqb_isoftlimit;
  2119. dm->dqb_ihardlimit = di->dqb_ihardlimit;
  2120. check_ilim = 1;
  2121. set_bit(DQ_LASTSET_B + QIF_ILIMITS_B, &dquot->dq_flags);
  2122. }
  2123. if (di->dqb_valid & QIF_BTIME) {
  2124. dm->dqb_btime = di->dqb_btime;
  2125. check_blim = 1;
  2126. set_bit(DQ_LASTSET_B + QIF_BTIME_B, &dquot->dq_flags);
  2127. }
  2128. if (di->dqb_valid & QIF_ITIME) {
  2129. dm->dqb_itime = di->dqb_itime;
  2130. check_ilim = 1;
  2131. set_bit(DQ_LASTSET_B + QIF_ITIME_B, &dquot->dq_flags);
  2132. }
  2133. if (check_blim) {
  2134. if (!dm->dqb_bsoftlimit ||
  2135. dm->dqb_curspace < dm->dqb_bsoftlimit) {
  2136. dm->dqb_btime = 0;
  2137. clear_bit(DQ_BLKS_B, &dquot->dq_flags);
  2138. } else if (!(di->dqb_valid & QIF_BTIME))
  2139. /* Set grace only if user hasn't provided his own... */
  2140. dm->dqb_btime = get_seconds() + dqi->dqi_bgrace;
  2141. }
  2142. if (check_ilim) {
  2143. if (!dm->dqb_isoftlimit ||
  2144. dm->dqb_curinodes < dm->dqb_isoftlimit) {
  2145. dm->dqb_itime = 0;
  2146. clear_bit(DQ_INODES_B, &dquot->dq_flags);
  2147. } else if (!(di->dqb_valid & QIF_ITIME))
  2148. /* Set grace only if user hasn't provided his own... */
  2149. dm->dqb_itime = get_seconds() + dqi->dqi_igrace;
  2150. }
  2151. if (dm->dqb_bhardlimit || dm->dqb_bsoftlimit || dm->dqb_ihardlimit ||
  2152. dm->dqb_isoftlimit)
  2153. clear_bit(DQ_FAKE_B, &dquot->dq_flags);
  2154. else
  2155. set_bit(DQ_FAKE_B, &dquot->dq_flags);
  2156. spin_unlock(&dq_data_lock);
  2157. mark_dquot_dirty(dquot);
  2158. return 0;
  2159. }
  2160. int vfs_set_dqblk(struct super_block *sb, int type, qid_t id,
  2161. struct if_dqblk *di)
  2162. {
  2163. struct dquot *dquot;
  2164. int rc;
  2165. dquot = dqget(sb, id, type);
  2166. if (!dquot) {
  2167. rc = -ESRCH;
  2168. goto out;
  2169. }
  2170. rc = do_set_dqblk(dquot, di);
  2171. dqput(dquot);
  2172. out:
  2173. return rc;
  2174. }
  2175. EXPORT_SYMBOL(vfs_set_dqblk);
  2176. /* Generic routine for getting common part of quota file information */
  2177. int vfs_get_dqinfo(struct super_block *sb, int type, struct if_dqinfo *ii)
  2178. {
  2179. struct mem_dqinfo *mi;
  2180. mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
  2181. if (!sb_has_quota_active(sb, type)) {
  2182. mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
  2183. return -ESRCH;
  2184. }
  2185. mi = sb_dqopt(sb)->info + type;
  2186. spin_lock(&dq_data_lock);
  2187. ii->dqi_bgrace = mi->dqi_bgrace;
  2188. ii->dqi_igrace = mi->dqi_igrace;
  2189. ii->dqi_flags = mi->dqi_flags & DQF_MASK;
  2190. ii->dqi_valid = IIF_ALL;
  2191. spin_unlock(&dq_data_lock);
  2192. mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
  2193. return 0;
  2194. }
  2195. EXPORT_SYMBOL(vfs_get_dqinfo);
  2196. /* Generic routine for setting common part of quota file information */
  2197. int vfs_set_dqinfo(struct super_block *sb, int type, struct if_dqinfo *ii)
  2198. {
  2199. struct mem_dqinfo *mi;
  2200. int err = 0;
  2201. mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
  2202. if (!sb_has_quota_active(sb, type)) {
  2203. err = -ESRCH;
  2204. goto out;
  2205. }
  2206. mi = sb_dqopt(sb)->info + type;
  2207. spin_lock(&dq_data_lock);
  2208. if (ii->dqi_valid & IIF_BGRACE)
  2209. mi->dqi_bgrace = ii->dqi_bgrace;
  2210. if (ii->dqi_valid & IIF_IGRACE)
  2211. mi->dqi_igrace = ii->dqi_igrace;
  2212. if (ii->dqi_valid & IIF_FLAGS)
  2213. mi->dqi_flags = (mi->dqi_flags & ~DQF_MASK) |
  2214. (ii->dqi_flags & DQF_MASK);
  2215. spin_unlock(&dq_data_lock);
  2216. mark_info_dirty(sb, type);
  2217. /* Force write to disk */
  2218. sb->dq_op->write_info(sb, type);
  2219. out:
  2220. mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
  2221. return err;
  2222. }
  2223. EXPORT_SYMBOL(vfs_set_dqinfo);
  2224. const struct quotactl_ops vfs_quotactl_ops = {
  2225. .quota_on = vfs_quota_on,
  2226. .quota_off = vfs_quota_off,
  2227. .quota_sync = vfs_quota_sync,
  2228. .get_info = vfs_get_dqinfo,
  2229. .set_info = vfs_set_dqinfo,
  2230. .get_dqblk = vfs_get_dqblk,
  2231. .set_dqblk = vfs_set_dqblk
  2232. };
  2233. static ctl_table fs_dqstats_table[] = {
  2234. {
  2235. .procname = "lookups",
  2236. .data = &dqstats.lookups,
  2237. .maxlen = sizeof(int),
  2238. .mode = 0444,
  2239. .proc_handler = proc_dointvec,
  2240. },
  2241. {
  2242. .procname = "drops",
  2243. .data = &dqstats.drops,
  2244. .maxlen = sizeof(int),
  2245. .mode = 0444,
  2246. .proc_handler = proc_dointvec,
  2247. },
  2248. {
  2249. .procname = "reads",
  2250. .data = &dqstats.reads,
  2251. .maxlen = sizeof(int),
  2252. .mode = 0444,
  2253. .proc_handler = proc_dointvec,
  2254. },
  2255. {
  2256. .procname = "writes",
  2257. .data = &dqstats.writes,
  2258. .maxlen = sizeof(int),
  2259. .mode = 0444,
  2260. .proc_handler = proc_dointvec,
  2261. },
  2262. {
  2263. .procname = "cache_hits",
  2264. .data = &dqstats.cache_hits,
  2265. .maxlen = sizeof(int),
  2266. .mode = 0444,
  2267. .proc_handler = proc_dointvec,
  2268. },
  2269. {
  2270. .procname = "allocated_dquots",
  2271. .data = &dqstats.allocated_dquots,
  2272. .maxlen = sizeof(int),
  2273. .mode = 0444,
  2274. .proc_handler = proc_dointvec,
  2275. },
  2276. {
  2277. .procname = "free_dquots",
  2278. .data = &dqstats.free_dquots,
  2279. .maxlen = sizeof(int),
  2280. .mode = 0444,
  2281. .proc_handler = proc_dointvec,
  2282. },
  2283. {
  2284. .procname = "syncs",
  2285. .data = &dqstats.syncs,
  2286. .maxlen = sizeof(int),
  2287. .mode = 0444,
  2288. .proc_handler = proc_dointvec,
  2289. },
  2290. #ifdef CONFIG_PRINT_QUOTA_WARNING
  2291. {
  2292. .procname = "warnings",
  2293. .data = &flag_print_warnings,
  2294. .maxlen = sizeof(int),
  2295. .mode = 0644,
  2296. .proc_handler = proc_dointvec,
  2297. },
  2298. #endif
  2299. { },
  2300. };
  2301. static ctl_table fs_table[] = {
  2302. {
  2303. .procname = "quota",
  2304. .mode = 0555,
  2305. .child = fs_dqstats_table,
  2306. },
  2307. { },
  2308. };
  2309. static ctl_table sys_table[] = {
  2310. {
  2311. .procname = "fs",
  2312. .mode = 0555,
  2313. .child = fs_table,
  2314. },
  2315. { },
  2316. };
  2317. static int __init dquot_init(void)
  2318. {
  2319. int i;
  2320. unsigned long nr_hash, order;
  2321. printk(KERN_NOTICE "VFS: Disk quotas %s\n", __DQUOT_VERSION__);
  2322. register_sysctl_table(sys_table);
  2323. dquot_cachep = kmem_cache_create("dquot",
  2324. sizeof(struct dquot), sizeof(unsigned long) * 4,
  2325. (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
  2326. SLAB_MEM_SPREAD|SLAB_PANIC),
  2327. NULL);
  2328. order = 0;
  2329. dquot_hash = (struct hlist_head *)__get_free_pages(GFP_ATOMIC, order);
  2330. if (!dquot_hash)
  2331. panic("Cannot create dquot hash table");
  2332. /* Find power-of-two hlist_heads which can fit into allocation */
  2333. nr_hash = (1UL << order) * PAGE_SIZE / sizeof(struct hlist_head);
  2334. dq_hash_bits = 0;
  2335. do {
  2336. dq_hash_bits++;
  2337. } while (nr_hash >> dq_hash_bits);
  2338. dq_hash_bits--;
  2339. nr_hash = 1UL << dq_hash_bits;
  2340. dq_hash_mask = nr_hash - 1;
  2341. for (i = 0; i < nr_hash; i++)
  2342. INIT_HLIST_HEAD(dquot_hash + i);
  2343. printk("Dquot-cache hash table entries: %ld (order %ld, %ld bytes)\n",
  2344. nr_hash, order, (PAGE_SIZE << order));
  2345. register_shrinker(&dqcache_shrinker);
  2346. return 0;
  2347. }
  2348. module_init(dquot_init);