dquot.c 70 KB

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