dquot.c 70 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571
  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. #define __DQUOT_PARANOIA
  81. /*
  82. * There are three quota SMP locks. dq_list_lock protects all lists with quotas
  83. * and quota formats, dqstats structure containing statistics about the lists
  84. * dq_data_lock protects data from dq_dqb and also mem_dqinfo structures and
  85. * also guards consistency of dquot->dq_dqb with inode->i_blocks, i_bytes.
  86. * i_blocks and i_bytes updates itself are guarded by i_lock acquired directly
  87. * in inode_add_bytes() and inode_sub_bytes(). dq_state_lock protects
  88. * modifications of quota state (on quotaon and quotaoff) and readers who care
  89. * about latest values take it as well.
  90. *
  91. * The spinlock ordering is hence: dq_data_lock > dq_list_lock > i_lock,
  92. * dq_list_lock > dq_state_lock
  93. *
  94. * Note that some things (eg. sb pointer, type, id) doesn't change during
  95. * the life of the dquot structure and so needn't to be protected by a lock
  96. *
  97. * Any operation working on dquots via inode pointers must hold dqptr_sem. If
  98. * operation is just reading pointers from inode (or not using them at all) the
  99. * read lock is enough. If pointers are altered function must hold write lock.
  100. * Special care needs to be taken about S_NOQUOTA inode flag (marking that
  101. * inode is a quota file). Functions adding pointers from inode to dquots have
  102. * to check this flag under dqptr_sem and then (if S_NOQUOTA is not set) they
  103. * have to do all pointer modifications before dropping dqptr_sem. This makes
  104. * sure they cannot race with quotaon which first sets S_NOQUOTA flag and
  105. * then drops all pointers to dquots from an inode.
  106. *
  107. * Each dquot has its dq_lock mutex. Locked dquots might not be referenced
  108. * from inodes (dquot_alloc_space() and such don't check the dq_lock).
  109. * Currently dquot is locked only when it is being read to memory (or space for
  110. * it is being allocated) on the first dqget() and when it is being released on
  111. * the last dqput(). The allocation and release oparations are serialized by
  112. * the dq_lock and by checking the use count in dquot_release(). Write
  113. * operations on dquots don't hold dq_lock as they copy data under dq_data_lock
  114. * spinlock to internal buffers before writing.
  115. *
  116. * Lock ordering (including related VFS locks) is the following:
  117. * i_mutex > dqonoff_sem > journal_lock > dqptr_sem > dquot->dq_lock >
  118. * dqio_mutex
  119. * The lock ordering of dqptr_sem imposed by quota code is only dqonoff_sem >
  120. * dqptr_sem. But filesystem has to count with the fact that functions such as
  121. * dquot_alloc_space() acquire dqptr_sem and they usually have to be called
  122. * from inside a transaction to keep filesystem consistency after a crash. Also
  123. * filesystems usually want to do some IO on dquot from ->mark_dirty which is
  124. * called with dqptr_sem held.
  125. * i_mutex on quota files is special (it's below dqio_mutex)
  126. */
  127. static __cacheline_aligned_in_smp DEFINE_SPINLOCK(dq_list_lock);
  128. static __cacheline_aligned_in_smp DEFINE_SPINLOCK(dq_state_lock);
  129. __cacheline_aligned_in_smp DEFINE_SPINLOCK(dq_data_lock);
  130. EXPORT_SYMBOL(dq_data_lock);
  131. static char *quotatypes[] = INITQFNAMES;
  132. static struct quota_format_type *quota_formats; /* List of registered formats */
  133. static struct quota_module_name module_names[] = INIT_QUOTA_MODULE_NAMES;
  134. /* SLAB cache for dquot structures */
  135. static struct kmem_cache *dquot_cachep;
  136. int register_quota_format(struct quota_format_type *fmt)
  137. {
  138. spin_lock(&dq_list_lock);
  139. fmt->qf_next = quota_formats;
  140. quota_formats = fmt;
  141. spin_unlock(&dq_list_lock);
  142. return 0;
  143. }
  144. EXPORT_SYMBOL(register_quota_format);
  145. void unregister_quota_format(struct quota_format_type *fmt)
  146. {
  147. struct quota_format_type **actqf;
  148. spin_lock(&dq_list_lock);
  149. for (actqf = &quota_formats; *actqf && *actqf != fmt;
  150. actqf = &(*actqf)->qf_next)
  151. ;
  152. if (*actqf)
  153. *actqf = (*actqf)->qf_next;
  154. spin_unlock(&dq_list_lock);
  155. }
  156. EXPORT_SYMBOL(unregister_quota_format);
  157. static struct quota_format_type *find_quota_format(int id)
  158. {
  159. struct quota_format_type *actqf;
  160. spin_lock(&dq_list_lock);
  161. for (actqf = quota_formats; actqf && actqf->qf_fmt_id != id;
  162. actqf = actqf->qf_next)
  163. ;
  164. if (!actqf || !try_module_get(actqf->qf_owner)) {
  165. int qm;
  166. spin_unlock(&dq_list_lock);
  167. for (qm = 0; module_names[qm].qm_fmt_id &&
  168. module_names[qm].qm_fmt_id != id; qm++)
  169. ;
  170. if (!module_names[qm].qm_fmt_id ||
  171. request_module(module_names[qm].qm_mod_name))
  172. return NULL;
  173. spin_lock(&dq_list_lock);
  174. for (actqf = quota_formats; actqf && actqf->qf_fmt_id != id;
  175. actqf = actqf->qf_next)
  176. ;
  177. if (actqf && !try_module_get(actqf->qf_owner))
  178. actqf = NULL;
  179. }
  180. spin_unlock(&dq_list_lock);
  181. return actqf;
  182. }
  183. static void put_quota_format(struct quota_format_type *fmt)
  184. {
  185. module_put(fmt->qf_owner);
  186. }
  187. /*
  188. * Dquot List Management:
  189. * The quota code uses three lists for dquot management: the inuse_list,
  190. * free_dquots, and dquot_hash[] array. A single dquot structure may be
  191. * on all three lists, depending on its current state.
  192. *
  193. * All dquots are placed to the end of inuse_list when first created, and this
  194. * list is used for invalidate operation, which must look at every dquot.
  195. *
  196. * Unused dquots (dq_count == 0) are added to the free_dquots list when freed,
  197. * and this list is searched whenever we need an available dquot. Dquots are
  198. * removed from the list as soon as they are used again, and
  199. * dqstats.free_dquots gives the number of dquots on the list. When
  200. * dquot is invalidated it's completely released from memory.
  201. *
  202. * Dquots with a specific identity (device, type and id) are placed on
  203. * one of the dquot_hash[] hash chains. The provides an efficient search
  204. * mechanism to locate a specific dquot.
  205. */
  206. static LIST_HEAD(inuse_list);
  207. static LIST_HEAD(free_dquots);
  208. static unsigned int dq_hash_bits, dq_hash_mask;
  209. static struct hlist_head *dquot_hash;
  210. struct dqstats dqstats;
  211. EXPORT_SYMBOL(dqstats);
  212. static qsize_t inode_get_rsv_space(struct inode *inode);
  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 __DQUOT_PARANOIA
  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 __DQUOT_PARANOIA
  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 __DQUOT_PARANOIA
  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. int reserved = 0;
  796. spin_lock(&inode_lock);
  797. list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
  798. if (inode->i_state & (I_FREEING|I_CLEAR|I_WILL_FREE|I_NEW))
  799. continue;
  800. if (unlikely(inode_get_rsv_space(inode) > 0))
  801. reserved = 1;
  802. if (!atomic_read(&inode->i_writecount))
  803. continue;
  804. if (!dqinit_needed(inode, type))
  805. continue;
  806. __iget(inode);
  807. spin_unlock(&inode_lock);
  808. iput(old_inode);
  809. sb->dq_op->initialize(inode, type);
  810. /* We hold a reference to 'inode' so it couldn't have been
  811. * removed from s_inodes list while we dropped the inode_lock.
  812. * We cannot iput the inode now as we can be holding the last
  813. * reference and we cannot iput it under inode_lock. So we
  814. * keep the reference and iput it later. */
  815. old_inode = inode;
  816. spin_lock(&inode_lock);
  817. }
  818. spin_unlock(&inode_lock);
  819. iput(old_inode);
  820. if (reserved) {
  821. printk(KERN_WARNING "VFS (%s): Writes happened before quota"
  822. " was turned on thus quota information is probably "
  823. "inconsistent. Please run quotacheck(8).\n", sb->s_id);
  824. }
  825. }
  826. /*
  827. * Return 0 if dqput() won't block.
  828. * (note that 1 doesn't necessarily mean blocking)
  829. */
  830. static inline int dqput_blocks(struct dquot *dquot)
  831. {
  832. if (atomic_read(&dquot->dq_count) <= 1)
  833. return 1;
  834. return 0;
  835. }
  836. /*
  837. * Remove references to dquots from inode and add dquot to list for freeing
  838. * if we have the last referece to dquot
  839. * We can't race with anybody because we hold dqptr_sem for writing...
  840. */
  841. static int remove_inode_dquot_ref(struct inode *inode, int type,
  842. struct list_head *tofree_head)
  843. {
  844. struct dquot *dquot = inode->i_dquot[type];
  845. inode->i_dquot[type] = NULL;
  846. if (dquot) {
  847. if (dqput_blocks(dquot)) {
  848. #ifdef __DQUOT_PARANOIA
  849. if (atomic_read(&dquot->dq_count) != 1)
  850. printk(KERN_WARNING "VFS: Adding dquot with dq_count %d to dispose list.\n", atomic_read(&dquot->dq_count));
  851. #endif
  852. spin_lock(&dq_list_lock);
  853. /* As dquot must have currently users it can't be on
  854. * the free list... */
  855. list_add(&dquot->dq_free, tofree_head);
  856. spin_unlock(&dq_list_lock);
  857. return 1;
  858. }
  859. else
  860. dqput(dquot); /* We have guaranteed we won't block */
  861. }
  862. return 0;
  863. }
  864. /*
  865. * Free list of dquots
  866. * Dquots are removed from inodes and no new references can be got so we are
  867. * the only ones holding reference
  868. */
  869. static void put_dquot_list(struct list_head *tofree_head)
  870. {
  871. struct list_head *act_head;
  872. struct dquot *dquot;
  873. act_head = tofree_head->next;
  874. while (act_head != tofree_head) {
  875. dquot = list_entry(act_head, struct dquot, dq_free);
  876. act_head = act_head->next;
  877. /* Remove dquot from the list so we won't have problems... */
  878. list_del_init(&dquot->dq_free);
  879. dqput(dquot);
  880. }
  881. }
  882. static void remove_dquot_ref(struct super_block *sb, int type,
  883. struct list_head *tofree_head)
  884. {
  885. struct inode *inode;
  886. spin_lock(&inode_lock);
  887. list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
  888. /*
  889. * We have to scan also I_NEW inodes because they can already
  890. * have quota pointer initialized. Luckily, we need to touch
  891. * only quota pointers and these have separate locking
  892. * (dqptr_sem).
  893. */
  894. if (!IS_NOQUOTA(inode))
  895. remove_inode_dquot_ref(inode, type, tofree_head);
  896. }
  897. spin_unlock(&inode_lock);
  898. }
  899. /* Gather all references from inodes and drop them */
  900. static void drop_dquot_ref(struct super_block *sb, int type)
  901. {
  902. LIST_HEAD(tofree_head);
  903. if (sb->dq_op) {
  904. down_write(&sb_dqopt(sb)->dqptr_sem);
  905. remove_dquot_ref(sb, type, &tofree_head);
  906. up_write(&sb_dqopt(sb)->dqptr_sem);
  907. put_dquot_list(&tofree_head);
  908. }
  909. }
  910. static inline void dquot_incr_inodes(struct dquot *dquot, qsize_t number)
  911. {
  912. dquot->dq_dqb.dqb_curinodes += number;
  913. }
  914. static inline void dquot_incr_space(struct dquot *dquot, qsize_t number)
  915. {
  916. dquot->dq_dqb.dqb_curspace += number;
  917. }
  918. static inline void dquot_resv_space(struct dquot *dquot, qsize_t number)
  919. {
  920. dquot->dq_dqb.dqb_rsvspace += number;
  921. }
  922. /*
  923. * Claim reserved quota space
  924. */
  925. static void dquot_claim_reserved_space(struct dquot *dquot, qsize_t number)
  926. {
  927. if (dquot->dq_dqb.dqb_rsvspace < number) {
  928. WARN_ON_ONCE(1);
  929. number = dquot->dq_dqb.dqb_rsvspace;
  930. }
  931. dquot->dq_dqb.dqb_curspace += number;
  932. dquot->dq_dqb.dqb_rsvspace -= number;
  933. }
  934. static inline
  935. void dquot_free_reserved_space(struct dquot *dquot, qsize_t number)
  936. {
  937. if (dquot->dq_dqb.dqb_rsvspace >= number)
  938. dquot->dq_dqb.dqb_rsvspace -= number;
  939. else {
  940. WARN_ON_ONCE(1);
  941. dquot->dq_dqb.dqb_rsvspace = 0;
  942. }
  943. }
  944. static void dquot_decr_inodes(struct dquot *dquot, qsize_t number)
  945. {
  946. if (sb_dqopt(dquot->dq_sb)->flags & DQUOT_NEGATIVE_USAGE ||
  947. dquot->dq_dqb.dqb_curinodes >= number)
  948. dquot->dq_dqb.dqb_curinodes -= number;
  949. else
  950. dquot->dq_dqb.dqb_curinodes = 0;
  951. if (dquot->dq_dqb.dqb_curinodes <= dquot->dq_dqb.dqb_isoftlimit)
  952. dquot->dq_dqb.dqb_itime = (time_t) 0;
  953. clear_bit(DQ_INODES_B, &dquot->dq_flags);
  954. }
  955. static void dquot_decr_space(struct dquot *dquot, qsize_t number)
  956. {
  957. if (sb_dqopt(dquot->dq_sb)->flags & DQUOT_NEGATIVE_USAGE ||
  958. dquot->dq_dqb.dqb_curspace >= number)
  959. dquot->dq_dqb.dqb_curspace -= number;
  960. else
  961. dquot->dq_dqb.dqb_curspace = 0;
  962. if (dquot->dq_dqb.dqb_curspace <= dquot->dq_dqb.dqb_bsoftlimit)
  963. dquot->dq_dqb.dqb_btime = (time_t) 0;
  964. clear_bit(DQ_BLKS_B, &dquot->dq_flags);
  965. }
  966. static int warning_issued(struct dquot *dquot, const int warntype)
  967. {
  968. int flag = (warntype == QUOTA_NL_BHARDWARN ||
  969. warntype == QUOTA_NL_BSOFTLONGWARN) ? DQ_BLKS_B :
  970. ((warntype == QUOTA_NL_IHARDWARN ||
  971. warntype == QUOTA_NL_ISOFTLONGWARN) ? DQ_INODES_B : 0);
  972. if (!flag)
  973. return 0;
  974. return test_and_set_bit(flag, &dquot->dq_flags);
  975. }
  976. #ifdef CONFIG_PRINT_QUOTA_WARNING
  977. static int flag_print_warnings = 1;
  978. static int need_print_warning(struct dquot *dquot)
  979. {
  980. if (!flag_print_warnings)
  981. return 0;
  982. switch (dquot->dq_type) {
  983. case USRQUOTA:
  984. return current_fsuid() == dquot->dq_id;
  985. case GRPQUOTA:
  986. return in_group_p(dquot->dq_id);
  987. }
  988. return 0;
  989. }
  990. /* Print warning to user which exceeded quota */
  991. static void print_warning(struct dquot *dquot, const int warntype)
  992. {
  993. char *msg = NULL;
  994. struct tty_struct *tty;
  995. if (warntype == QUOTA_NL_IHARDBELOW ||
  996. warntype == QUOTA_NL_ISOFTBELOW ||
  997. warntype == QUOTA_NL_BHARDBELOW ||
  998. warntype == QUOTA_NL_BSOFTBELOW || !need_print_warning(dquot))
  999. return;
  1000. tty = get_current_tty();
  1001. if (!tty)
  1002. return;
  1003. tty_write_message(tty, dquot->dq_sb->s_id);
  1004. if (warntype == QUOTA_NL_ISOFTWARN || warntype == QUOTA_NL_BSOFTWARN)
  1005. tty_write_message(tty, ": warning, ");
  1006. else
  1007. tty_write_message(tty, ": write failed, ");
  1008. tty_write_message(tty, quotatypes[dquot->dq_type]);
  1009. switch (warntype) {
  1010. case QUOTA_NL_IHARDWARN:
  1011. msg = " file limit reached.\r\n";
  1012. break;
  1013. case QUOTA_NL_ISOFTLONGWARN:
  1014. msg = " file quota exceeded too long.\r\n";
  1015. break;
  1016. case QUOTA_NL_ISOFTWARN:
  1017. msg = " file quota exceeded.\r\n";
  1018. break;
  1019. case QUOTA_NL_BHARDWARN:
  1020. msg = " block limit reached.\r\n";
  1021. break;
  1022. case QUOTA_NL_BSOFTLONGWARN:
  1023. msg = " block quota exceeded too long.\r\n";
  1024. break;
  1025. case QUOTA_NL_BSOFTWARN:
  1026. msg = " block quota exceeded.\r\n";
  1027. break;
  1028. }
  1029. tty_write_message(tty, msg);
  1030. tty_kref_put(tty);
  1031. }
  1032. #endif
  1033. /*
  1034. * Write warnings to the console and send warning messages over netlink.
  1035. *
  1036. * Note that this function can sleep.
  1037. */
  1038. static void flush_warnings(struct dquot *const *dquots, char *warntype)
  1039. {
  1040. struct dquot *dq;
  1041. int i;
  1042. for (i = 0; i < MAXQUOTAS; i++) {
  1043. dq = dquots[i];
  1044. if (dq && warntype[i] != QUOTA_NL_NOWARN &&
  1045. !warning_issued(dq, warntype[i])) {
  1046. #ifdef CONFIG_PRINT_QUOTA_WARNING
  1047. print_warning(dq, warntype[i]);
  1048. #endif
  1049. quota_send_warning(dq->dq_type, dq->dq_id,
  1050. dq->dq_sb->s_dev, warntype[i]);
  1051. }
  1052. }
  1053. }
  1054. static int ignore_hardlimit(struct dquot *dquot)
  1055. {
  1056. struct mem_dqinfo *info = &sb_dqopt(dquot->dq_sb)->info[dquot->dq_type];
  1057. return capable(CAP_SYS_RESOURCE) &&
  1058. (info->dqi_format->qf_fmt_id != QFMT_VFS_OLD ||
  1059. !(info->dqi_flags & V1_DQF_RSQUASH));
  1060. }
  1061. /* needs dq_data_lock */
  1062. static int check_idq(struct dquot *dquot, qsize_t inodes, char *warntype)
  1063. {
  1064. qsize_t newinodes = dquot->dq_dqb.dqb_curinodes + inodes;
  1065. *warntype = QUOTA_NL_NOWARN;
  1066. if (!sb_has_quota_limits_enabled(dquot->dq_sb, dquot->dq_type) ||
  1067. test_bit(DQ_FAKE_B, &dquot->dq_flags))
  1068. return QUOTA_OK;
  1069. if (dquot->dq_dqb.dqb_ihardlimit &&
  1070. newinodes > dquot->dq_dqb.dqb_ihardlimit &&
  1071. !ignore_hardlimit(dquot)) {
  1072. *warntype = QUOTA_NL_IHARDWARN;
  1073. return NO_QUOTA;
  1074. }
  1075. if (dquot->dq_dqb.dqb_isoftlimit &&
  1076. newinodes > dquot->dq_dqb.dqb_isoftlimit &&
  1077. dquot->dq_dqb.dqb_itime &&
  1078. get_seconds() >= dquot->dq_dqb.dqb_itime &&
  1079. !ignore_hardlimit(dquot)) {
  1080. *warntype = QUOTA_NL_ISOFTLONGWARN;
  1081. return NO_QUOTA;
  1082. }
  1083. if (dquot->dq_dqb.dqb_isoftlimit &&
  1084. newinodes > dquot->dq_dqb.dqb_isoftlimit &&
  1085. dquot->dq_dqb.dqb_itime == 0) {
  1086. *warntype = QUOTA_NL_ISOFTWARN;
  1087. dquot->dq_dqb.dqb_itime = get_seconds() +
  1088. sb_dqopt(dquot->dq_sb)->info[dquot->dq_type].dqi_igrace;
  1089. }
  1090. return QUOTA_OK;
  1091. }
  1092. /* needs dq_data_lock */
  1093. static int check_bdq(struct dquot *dquot, qsize_t space, int prealloc, char *warntype)
  1094. {
  1095. qsize_t tspace;
  1096. struct super_block *sb = dquot->dq_sb;
  1097. *warntype = QUOTA_NL_NOWARN;
  1098. if (!sb_has_quota_limits_enabled(sb, dquot->dq_type) ||
  1099. test_bit(DQ_FAKE_B, &dquot->dq_flags))
  1100. return QUOTA_OK;
  1101. tspace = dquot->dq_dqb.dqb_curspace + dquot->dq_dqb.dqb_rsvspace
  1102. + space;
  1103. if (dquot->dq_dqb.dqb_bhardlimit &&
  1104. tspace > dquot->dq_dqb.dqb_bhardlimit &&
  1105. !ignore_hardlimit(dquot)) {
  1106. if (!prealloc)
  1107. *warntype = QUOTA_NL_BHARDWARN;
  1108. return NO_QUOTA;
  1109. }
  1110. if (dquot->dq_dqb.dqb_bsoftlimit &&
  1111. tspace > dquot->dq_dqb.dqb_bsoftlimit &&
  1112. dquot->dq_dqb.dqb_btime &&
  1113. get_seconds() >= dquot->dq_dqb.dqb_btime &&
  1114. !ignore_hardlimit(dquot)) {
  1115. if (!prealloc)
  1116. *warntype = QUOTA_NL_BSOFTLONGWARN;
  1117. return NO_QUOTA;
  1118. }
  1119. if (dquot->dq_dqb.dqb_bsoftlimit &&
  1120. tspace > dquot->dq_dqb.dqb_bsoftlimit &&
  1121. dquot->dq_dqb.dqb_btime == 0) {
  1122. if (!prealloc) {
  1123. *warntype = QUOTA_NL_BSOFTWARN;
  1124. dquot->dq_dqb.dqb_btime = get_seconds() +
  1125. sb_dqopt(sb)->info[dquot->dq_type].dqi_bgrace;
  1126. }
  1127. else
  1128. /*
  1129. * We don't allow preallocation to exceed softlimit so exceeding will
  1130. * be always printed
  1131. */
  1132. return NO_QUOTA;
  1133. }
  1134. return QUOTA_OK;
  1135. }
  1136. static int info_idq_free(struct dquot *dquot, qsize_t inodes)
  1137. {
  1138. qsize_t newinodes;
  1139. if (test_bit(DQ_FAKE_B, &dquot->dq_flags) ||
  1140. dquot->dq_dqb.dqb_curinodes <= dquot->dq_dqb.dqb_isoftlimit ||
  1141. !sb_has_quota_limits_enabled(dquot->dq_sb, dquot->dq_type))
  1142. return QUOTA_NL_NOWARN;
  1143. newinodes = dquot->dq_dqb.dqb_curinodes - inodes;
  1144. if (newinodes <= dquot->dq_dqb.dqb_isoftlimit)
  1145. return QUOTA_NL_ISOFTBELOW;
  1146. if (dquot->dq_dqb.dqb_curinodes >= dquot->dq_dqb.dqb_ihardlimit &&
  1147. newinodes < dquot->dq_dqb.dqb_ihardlimit)
  1148. return QUOTA_NL_IHARDBELOW;
  1149. return QUOTA_NL_NOWARN;
  1150. }
  1151. static int info_bdq_free(struct dquot *dquot, qsize_t space)
  1152. {
  1153. if (test_bit(DQ_FAKE_B, &dquot->dq_flags) ||
  1154. dquot->dq_dqb.dqb_curspace <= dquot->dq_dqb.dqb_bsoftlimit)
  1155. return QUOTA_NL_NOWARN;
  1156. if (dquot->dq_dqb.dqb_curspace - space <= dquot->dq_dqb.dqb_bsoftlimit)
  1157. return QUOTA_NL_BSOFTBELOW;
  1158. if (dquot->dq_dqb.dqb_curspace >= dquot->dq_dqb.dqb_bhardlimit &&
  1159. dquot->dq_dqb.dqb_curspace - space < dquot->dq_dqb.dqb_bhardlimit)
  1160. return QUOTA_NL_BHARDBELOW;
  1161. return QUOTA_NL_NOWARN;
  1162. }
  1163. /*
  1164. * Initialize quota pointers in inode
  1165. * We do things in a bit complicated way but by that we avoid calling
  1166. * dqget() and thus filesystem callbacks under dqptr_sem.
  1167. */
  1168. int dquot_initialize(struct inode *inode, int type)
  1169. {
  1170. unsigned int id = 0;
  1171. int cnt, ret = 0;
  1172. struct dquot *got[MAXQUOTAS];
  1173. struct super_block *sb = inode->i_sb;
  1174. qsize_t rsv;
  1175. /* First test before acquiring mutex - solves deadlocks when we
  1176. * re-enter the quota code and are already holding the mutex */
  1177. if (IS_NOQUOTA(inode))
  1178. return 0;
  1179. /* First get references to structures we might need. */
  1180. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1181. got[cnt] = NULL;
  1182. if (type != -1 && cnt != type)
  1183. continue;
  1184. switch (cnt) {
  1185. case USRQUOTA:
  1186. id = inode->i_uid;
  1187. break;
  1188. case GRPQUOTA:
  1189. id = inode->i_gid;
  1190. break;
  1191. }
  1192. got[cnt] = dqget(sb, id, cnt);
  1193. }
  1194. down_write(&sb_dqopt(sb)->dqptr_sem);
  1195. if (IS_NOQUOTA(inode))
  1196. goto out_err;
  1197. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1198. if (type != -1 && cnt != type)
  1199. continue;
  1200. /* Avoid races with quotaoff() */
  1201. if (!sb_has_quota_active(sb, cnt))
  1202. continue;
  1203. if (!inode->i_dquot[cnt]) {
  1204. inode->i_dquot[cnt] = got[cnt];
  1205. got[cnt] = NULL;
  1206. /*
  1207. * Make quota reservation system happy if someone
  1208. * did a write before quota was turned on
  1209. */
  1210. rsv = inode_get_rsv_space(inode);
  1211. if (unlikely(rsv))
  1212. dquot_resv_space(inode->i_dquot[cnt], rsv);
  1213. }
  1214. }
  1215. out_err:
  1216. up_write(&sb_dqopt(sb)->dqptr_sem);
  1217. /* Drop unused references */
  1218. dqput_all(got);
  1219. return ret;
  1220. }
  1221. EXPORT_SYMBOL(dquot_initialize);
  1222. /*
  1223. * Release all quotas referenced by inode
  1224. */
  1225. int dquot_drop(struct inode *inode)
  1226. {
  1227. int cnt;
  1228. struct dquot *put[MAXQUOTAS];
  1229. down_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1230. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1231. put[cnt] = inode->i_dquot[cnt];
  1232. inode->i_dquot[cnt] = NULL;
  1233. }
  1234. up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1235. dqput_all(put);
  1236. return 0;
  1237. }
  1238. EXPORT_SYMBOL(dquot_drop);
  1239. /* Wrapper to remove references to quota structures from inode */
  1240. void vfs_dq_drop(struct inode *inode)
  1241. {
  1242. /* Here we can get arbitrary inode from clear_inode() so we have
  1243. * to be careful. OTOH we don't need locking as quota operations
  1244. * are allowed to change only at mount time */
  1245. if (!IS_NOQUOTA(inode) && inode->i_sb && inode->i_sb->dq_op
  1246. && inode->i_sb->dq_op->drop) {
  1247. int cnt;
  1248. /* Test before calling to rule out calls from proc and such
  1249. * where we are not allowed to block. Note that this is
  1250. * actually reliable test even without the lock - the caller
  1251. * must assure that nobody can come after the DQUOT_DROP and
  1252. * add quota pointers back anyway */
  1253. for (cnt = 0; cnt < MAXQUOTAS; cnt++)
  1254. if (inode->i_dquot[cnt])
  1255. break;
  1256. if (cnt < MAXQUOTAS)
  1257. inode->i_sb->dq_op->drop(inode);
  1258. }
  1259. }
  1260. EXPORT_SYMBOL(vfs_dq_drop);
  1261. /*
  1262. * inode_reserved_space is managed internally by quota, and protected by
  1263. * i_lock similar to i_blocks+i_bytes.
  1264. */
  1265. static qsize_t *inode_reserved_space(struct inode * inode)
  1266. {
  1267. /* Filesystem must explicitly define it's own method in order to use
  1268. * quota reservation interface */
  1269. BUG_ON(!inode->i_sb->dq_op->get_reserved_space);
  1270. return inode->i_sb->dq_op->get_reserved_space(inode);
  1271. }
  1272. void inode_add_rsv_space(struct inode *inode, qsize_t number)
  1273. {
  1274. spin_lock(&inode->i_lock);
  1275. *inode_reserved_space(inode) += number;
  1276. spin_unlock(&inode->i_lock);
  1277. }
  1278. EXPORT_SYMBOL(inode_add_rsv_space);
  1279. void inode_claim_rsv_space(struct inode *inode, qsize_t number)
  1280. {
  1281. spin_lock(&inode->i_lock);
  1282. *inode_reserved_space(inode) -= number;
  1283. __inode_add_bytes(inode, number);
  1284. spin_unlock(&inode->i_lock);
  1285. }
  1286. EXPORT_SYMBOL(inode_claim_rsv_space);
  1287. void inode_sub_rsv_space(struct inode *inode, qsize_t number)
  1288. {
  1289. spin_lock(&inode->i_lock);
  1290. *inode_reserved_space(inode) -= number;
  1291. spin_unlock(&inode->i_lock);
  1292. }
  1293. EXPORT_SYMBOL(inode_sub_rsv_space);
  1294. static qsize_t inode_get_rsv_space(struct inode *inode)
  1295. {
  1296. qsize_t ret;
  1297. if (!inode->i_sb->dq_op->get_reserved_space)
  1298. return 0;
  1299. spin_lock(&inode->i_lock);
  1300. ret = *inode_reserved_space(inode);
  1301. spin_unlock(&inode->i_lock);
  1302. return ret;
  1303. }
  1304. static void inode_incr_space(struct inode *inode, qsize_t number,
  1305. int reserve)
  1306. {
  1307. if (reserve)
  1308. inode_add_rsv_space(inode, number);
  1309. else
  1310. inode_add_bytes(inode, number);
  1311. }
  1312. static void inode_decr_space(struct inode *inode, qsize_t number, int reserve)
  1313. {
  1314. if (reserve)
  1315. inode_sub_rsv_space(inode, number);
  1316. else
  1317. inode_sub_bytes(inode, number);
  1318. }
  1319. /*
  1320. * This functions updates i_blocks+i_bytes fields and quota information
  1321. * (together with appropriate checks).
  1322. *
  1323. * NOTE: We absolutely rely on the fact that caller dirties the inode
  1324. * (usually helpers in quotaops.h care about this) and holds a handle for
  1325. * the current transaction so that dquot write and inode write go into the
  1326. * same transaction.
  1327. */
  1328. /*
  1329. * This operation can block, but only after everything is updated
  1330. */
  1331. int __dquot_alloc_space(struct inode *inode, qsize_t number,
  1332. int warn, int reserve)
  1333. {
  1334. int cnt, ret = 0;
  1335. char warntype[MAXQUOTAS];
  1336. /*
  1337. * First test before acquiring mutex - solves deadlocks when we
  1338. * re-enter the quota code and are already holding the mutex
  1339. */
  1340. if (!sb_any_quota_active(inode->i_sb) || IS_NOQUOTA(inode)) {
  1341. inode_incr_space(inode, number, reserve);
  1342. goto out;
  1343. }
  1344. down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1345. for (cnt = 0; cnt < MAXQUOTAS; cnt++)
  1346. warntype[cnt] = QUOTA_NL_NOWARN;
  1347. spin_lock(&dq_data_lock);
  1348. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1349. if (!inode->i_dquot[cnt])
  1350. continue;
  1351. if (check_bdq(inode->i_dquot[cnt], number, !warn, warntype+cnt)
  1352. == NO_QUOTA) {
  1353. ret = -EDQUOT;
  1354. spin_unlock(&dq_data_lock);
  1355. goto out_flush_warn;
  1356. }
  1357. }
  1358. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1359. if (!inode->i_dquot[cnt])
  1360. continue;
  1361. if (reserve)
  1362. dquot_resv_space(inode->i_dquot[cnt], number);
  1363. else
  1364. dquot_incr_space(inode->i_dquot[cnt], number);
  1365. }
  1366. inode_incr_space(inode, number, reserve);
  1367. spin_unlock(&dq_data_lock);
  1368. if (reserve)
  1369. goto out_flush_warn;
  1370. mark_all_dquot_dirty(inode->i_dquot);
  1371. out_flush_warn:
  1372. flush_warnings(inode->i_dquot, warntype);
  1373. up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1374. out:
  1375. return ret;
  1376. }
  1377. EXPORT_SYMBOL(__dquot_alloc_space);
  1378. /*
  1379. * This operation can block, but only after everything is updated
  1380. */
  1381. int dquot_alloc_inode(const struct inode *inode)
  1382. {
  1383. int cnt, ret = -EDQUOT;
  1384. char warntype[MAXQUOTAS];
  1385. /* First test before acquiring mutex - solves deadlocks when we
  1386. * re-enter the quota code and are already holding the mutex */
  1387. if (!sb_any_quota_active(inode->i_sb) || IS_NOQUOTA(inode))
  1388. return 0;
  1389. for (cnt = 0; cnt < MAXQUOTAS; cnt++)
  1390. warntype[cnt] = QUOTA_NL_NOWARN;
  1391. down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1392. spin_lock(&dq_data_lock);
  1393. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1394. if (!inode->i_dquot[cnt])
  1395. continue;
  1396. if (check_idq(inode->i_dquot[cnt], 1, warntype+cnt)
  1397. == NO_QUOTA)
  1398. goto warn_put_all;
  1399. }
  1400. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1401. if (!inode->i_dquot[cnt])
  1402. continue;
  1403. dquot_incr_inodes(inode->i_dquot[cnt], 1);
  1404. }
  1405. ret = 0;
  1406. warn_put_all:
  1407. spin_unlock(&dq_data_lock);
  1408. if (ret == 0)
  1409. mark_all_dquot_dirty(inode->i_dquot);
  1410. flush_warnings(inode->i_dquot, warntype);
  1411. up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1412. return ret;
  1413. }
  1414. EXPORT_SYMBOL(dquot_alloc_inode);
  1415. /*
  1416. * Convert in-memory reserved quotas to real consumed quotas
  1417. */
  1418. int dquot_claim_space_nodirty(struct inode *inode, qsize_t number)
  1419. {
  1420. int cnt;
  1421. if (!sb_any_quota_active(inode->i_sb) || IS_NOQUOTA(inode)) {
  1422. inode_claim_rsv_space(inode, number);
  1423. return 0;
  1424. }
  1425. down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1426. spin_lock(&dq_data_lock);
  1427. /* Claim reserved quotas to allocated quotas */
  1428. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1429. if (inode->i_dquot[cnt])
  1430. dquot_claim_reserved_space(inode->i_dquot[cnt],
  1431. number);
  1432. }
  1433. /* Update inode bytes */
  1434. inode_claim_rsv_space(inode, number);
  1435. spin_unlock(&dq_data_lock);
  1436. mark_all_dquot_dirty(inode->i_dquot);
  1437. up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1438. return 0;
  1439. }
  1440. EXPORT_SYMBOL(dquot_claim_space_nodirty);
  1441. /*
  1442. * This operation can block, but only after everything is updated
  1443. */
  1444. void __dquot_free_space(struct inode *inode, qsize_t number, int reserve)
  1445. {
  1446. unsigned int cnt;
  1447. char warntype[MAXQUOTAS];
  1448. /* First test before acquiring mutex - solves deadlocks when we
  1449. * re-enter the quota code and are already holding the mutex */
  1450. if (!sb_any_quota_active(inode->i_sb) || IS_NOQUOTA(inode)) {
  1451. inode_decr_space(inode, number, reserve);
  1452. return;
  1453. }
  1454. down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1455. spin_lock(&dq_data_lock);
  1456. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1457. if (!inode->i_dquot[cnt])
  1458. continue;
  1459. warntype[cnt] = info_bdq_free(inode->i_dquot[cnt], number);
  1460. if (reserve)
  1461. dquot_free_reserved_space(inode->i_dquot[cnt], number);
  1462. else
  1463. dquot_decr_space(inode->i_dquot[cnt], number);
  1464. }
  1465. inode_decr_space(inode, number, reserve);
  1466. spin_unlock(&dq_data_lock);
  1467. if (reserve)
  1468. goto out_unlock;
  1469. mark_all_dquot_dirty(inode->i_dquot);
  1470. out_unlock:
  1471. flush_warnings(inode->i_dquot, warntype);
  1472. up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1473. }
  1474. EXPORT_SYMBOL(__dquot_free_space);
  1475. /*
  1476. * This operation can block, but only after everything is updated
  1477. */
  1478. void dquot_free_inode(const struct inode *inode)
  1479. {
  1480. unsigned int cnt;
  1481. char warntype[MAXQUOTAS];
  1482. /* First test before acquiring mutex - solves deadlocks when we
  1483. * re-enter the quota code and are already holding the mutex */
  1484. if (!sb_any_quota_active(inode->i_sb) || IS_NOQUOTA(inode))
  1485. return;
  1486. down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1487. spin_lock(&dq_data_lock);
  1488. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1489. if (!inode->i_dquot[cnt])
  1490. continue;
  1491. warntype[cnt] = info_idq_free(inode->i_dquot[cnt], 1);
  1492. dquot_decr_inodes(inode->i_dquot[cnt], 1);
  1493. }
  1494. spin_unlock(&dq_data_lock);
  1495. mark_all_dquot_dirty(inode->i_dquot);
  1496. flush_warnings(inode->i_dquot, warntype);
  1497. up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1498. }
  1499. EXPORT_SYMBOL(dquot_free_inode);
  1500. /*
  1501. * Transfer the number of inode and blocks from one diskquota to an other.
  1502. *
  1503. * This operation can block, but only after everything is updated
  1504. * A transaction must be started when entering this function.
  1505. */
  1506. static int __dquot_transfer(struct inode *inode, qid_t *chid, unsigned long mask)
  1507. {
  1508. qsize_t space, cur_space;
  1509. qsize_t rsv_space = 0;
  1510. struct dquot *transfer_from[MAXQUOTAS];
  1511. struct dquot *transfer_to[MAXQUOTAS];
  1512. int cnt, ret = QUOTA_OK;
  1513. char warntype_to[MAXQUOTAS];
  1514. char warntype_from_inodes[MAXQUOTAS], warntype_from_space[MAXQUOTAS];
  1515. /* First test before acquiring mutex - solves deadlocks when we
  1516. * re-enter the quota code and are already holding the mutex */
  1517. if (IS_NOQUOTA(inode))
  1518. return QUOTA_OK;
  1519. /* Initialize the arrays */
  1520. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1521. transfer_from[cnt] = NULL;
  1522. transfer_to[cnt] = NULL;
  1523. warntype_to[cnt] = QUOTA_NL_NOWARN;
  1524. }
  1525. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1526. if (mask & (1 << cnt))
  1527. transfer_to[cnt] = dqget(inode->i_sb, chid[cnt], cnt);
  1528. }
  1529. down_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1530. if (IS_NOQUOTA(inode)) { /* File without quota accounting? */
  1531. up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1532. goto put_all;
  1533. }
  1534. spin_lock(&dq_data_lock);
  1535. cur_space = inode_get_bytes(inode);
  1536. rsv_space = inode_get_rsv_space(inode);
  1537. space = cur_space + rsv_space;
  1538. /* Build the transfer_from list and check the limits */
  1539. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1540. if (!transfer_to[cnt])
  1541. continue;
  1542. transfer_from[cnt] = inode->i_dquot[cnt];
  1543. if (check_idq(transfer_to[cnt], 1, warntype_to + cnt) ==
  1544. NO_QUOTA || check_bdq(transfer_to[cnt], space, 0,
  1545. warntype_to + cnt) == NO_QUOTA)
  1546. goto over_quota;
  1547. }
  1548. /*
  1549. * Finally perform the needed transfer from transfer_from to transfer_to
  1550. */
  1551. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1552. /*
  1553. * Skip changes for same uid or gid or for turned off quota-type.
  1554. */
  1555. if (!transfer_to[cnt])
  1556. continue;
  1557. /* Due to IO error we might not have transfer_from[] structure */
  1558. if (transfer_from[cnt]) {
  1559. warntype_from_inodes[cnt] =
  1560. info_idq_free(transfer_from[cnt], 1);
  1561. warntype_from_space[cnt] =
  1562. info_bdq_free(transfer_from[cnt], space);
  1563. dquot_decr_inodes(transfer_from[cnt], 1);
  1564. dquot_decr_space(transfer_from[cnt], cur_space);
  1565. dquot_free_reserved_space(transfer_from[cnt],
  1566. rsv_space);
  1567. }
  1568. dquot_incr_inodes(transfer_to[cnt], 1);
  1569. dquot_incr_space(transfer_to[cnt], cur_space);
  1570. dquot_resv_space(transfer_to[cnt], rsv_space);
  1571. inode->i_dquot[cnt] = transfer_to[cnt];
  1572. }
  1573. spin_unlock(&dq_data_lock);
  1574. up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1575. mark_all_dquot_dirty(transfer_from);
  1576. mark_all_dquot_dirty(transfer_to);
  1577. /* The reference we got is transferred to the inode */
  1578. for (cnt = 0; cnt < MAXQUOTAS; cnt++)
  1579. transfer_to[cnt] = NULL;
  1580. warn_put_all:
  1581. flush_warnings(transfer_to, warntype_to);
  1582. flush_warnings(transfer_from, warntype_from_inodes);
  1583. flush_warnings(transfer_from, warntype_from_space);
  1584. put_all:
  1585. dqput_all(transfer_from);
  1586. dqput_all(transfer_to);
  1587. return ret;
  1588. over_quota:
  1589. spin_unlock(&dq_data_lock);
  1590. up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1591. /* Clear dquot pointers we don't want to dqput() */
  1592. for (cnt = 0; cnt < MAXQUOTAS; cnt++)
  1593. transfer_from[cnt] = NULL;
  1594. ret = NO_QUOTA;
  1595. goto warn_put_all;
  1596. }
  1597. /* Wrapper for transferring ownership of an inode for uid/gid only
  1598. * Called from FSXXX_setattr()
  1599. */
  1600. int dquot_transfer(struct inode *inode, struct iattr *iattr)
  1601. {
  1602. qid_t chid[MAXQUOTAS];
  1603. unsigned long mask = 0;
  1604. if (iattr->ia_valid & ATTR_UID && iattr->ia_uid != inode->i_uid) {
  1605. mask |= 1 << USRQUOTA;
  1606. chid[USRQUOTA] = iattr->ia_uid;
  1607. }
  1608. if (iattr->ia_valid & ATTR_GID && iattr->ia_gid != inode->i_gid) {
  1609. mask |= 1 << GRPQUOTA;
  1610. chid[GRPQUOTA] = iattr->ia_gid;
  1611. }
  1612. if (sb_any_quota_active(inode->i_sb) && !IS_NOQUOTA(inode)) {
  1613. vfs_dq_init(inode);
  1614. if (__dquot_transfer(inode, chid, mask) == NO_QUOTA)
  1615. return -EDQUOT;
  1616. }
  1617. return 0;
  1618. }
  1619. EXPORT_SYMBOL(dquot_transfer);
  1620. /*
  1621. * Write info of quota file to disk
  1622. */
  1623. int dquot_commit_info(struct super_block *sb, int type)
  1624. {
  1625. int ret;
  1626. struct quota_info *dqopt = sb_dqopt(sb);
  1627. mutex_lock(&dqopt->dqio_mutex);
  1628. ret = dqopt->ops[type]->write_file_info(sb, type);
  1629. mutex_unlock(&dqopt->dqio_mutex);
  1630. return ret;
  1631. }
  1632. EXPORT_SYMBOL(dquot_commit_info);
  1633. /*
  1634. * Definitions of diskquota operations.
  1635. */
  1636. const struct dquot_operations dquot_operations = {
  1637. .initialize = dquot_initialize,
  1638. .drop = dquot_drop,
  1639. .write_dquot = dquot_commit,
  1640. .acquire_dquot = dquot_acquire,
  1641. .release_dquot = dquot_release,
  1642. .mark_dirty = dquot_mark_dquot_dirty,
  1643. .write_info = dquot_commit_info,
  1644. .alloc_dquot = dquot_alloc,
  1645. .destroy_dquot = dquot_destroy,
  1646. };
  1647. /*
  1648. * Turn quota off on a device. type == -1 ==> quotaoff for all types (umount)
  1649. */
  1650. int vfs_quota_disable(struct super_block *sb, int type, unsigned int flags)
  1651. {
  1652. int cnt, ret = 0;
  1653. struct quota_info *dqopt = sb_dqopt(sb);
  1654. struct inode *toputinode[MAXQUOTAS];
  1655. /* Cannot turn off usage accounting without turning off limits, or
  1656. * suspend quotas and simultaneously turn quotas off. */
  1657. if ((flags & DQUOT_USAGE_ENABLED && !(flags & DQUOT_LIMITS_ENABLED))
  1658. || (flags & DQUOT_SUSPENDED && flags & (DQUOT_LIMITS_ENABLED |
  1659. DQUOT_USAGE_ENABLED)))
  1660. return -EINVAL;
  1661. /* We need to serialize quota_off() for device */
  1662. mutex_lock(&dqopt->dqonoff_mutex);
  1663. /*
  1664. * Skip everything if there's nothing to do. We have to do this because
  1665. * sometimes we are called when fill_super() failed and calling
  1666. * sync_fs() in such cases does no good.
  1667. */
  1668. if (!sb_any_quota_loaded(sb)) {
  1669. mutex_unlock(&dqopt->dqonoff_mutex);
  1670. return 0;
  1671. }
  1672. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1673. toputinode[cnt] = NULL;
  1674. if (type != -1 && cnt != type)
  1675. continue;
  1676. if (!sb_has_quota_loaded(sb, cnt))
  1677. continue;
  1678. if (flags & DQUOT_SUSPENDED) {
  1679. spin_lock(&dq_state_lock);
  1680. dqopt->flags |=
  1681. dquot_state_flag(DQUOT_SUSPENDED, cnt);
  1682. spin_unlock(&dq_state_lock);
  1683. } else {
  1684. spin_lock(&dq_state_lock);
  1685. dqopt->flags &= ~dquot_state_flag(flags, cnt);
  1686. /* Turning off suspended quotas? */
  1687. if (!sb_has_quota_loaded(sb, cnt) &&
  1688. sb_has_quota_suspended(sb, cnt)) {
  1689. dqopt->flags &= ~dquot_state_flag(
  1690. DQUOT_SUSPENDED, cnt);
  1691. spin_unlock(&dq_state_lock);
  1692. iput(dqopt->files[cnt]);
  1693. dqopt->files[cnt] = NULL;
  1694. continue;
  1695. }
  1696. spin_unlock(&dq_state_lock);
  1697. }
  1698. /* We still have to keep quota loaded? */
  1699. if (sb_has_quota_loaded(sb, cnt) && !(flags & DQUOT_SUSPENDED))
  1700. continue;
  1701. /* Note: these are blocking operations */
  1702. drop_dquot_ref(sb, cnt);
  1703. invalidate_dquots(sb, cnt);
  1704. /*
  1705. * Now all dquots should be invalidated, all writes done so we
  1706. * should be only users of the info. No locks needed.
  1707. */
  1708. if (info_dirty(&dqopt->info[cnt]))
  1709. sb->dq_op->write_info(sb, cnt);
  1710. if (dqopt->ops[cnt]->free_file_info)
  1711. dqopt->ops[cnt]->free_file_info(sb, cnt);
  1712. put_quota_format(dqopt->info[cnt].dqi_format);
  1713. toputinode[cnt] = dqopt->files[cnt];
  1714. if (!sb_has_quota_loaded(sb, cnt))
  1715. dqopt->files[cnt] = NULL;
  1716. dqopt->info[cnt].dqi_flags = 0;
  1717. dqopt->info[cnt].dqi_igrace = 0;
  1718. dqopt->info[cnt].dqi_bgrace = 0;
  1719. dqopt->ops[cnt] = NULL;
  1720. }
  1721. mutex_unlock(&dqopt->dqonoff_mutex);
  1722. /* Skip syncing and setting flags if quota files are hidden */
  1723. if (dqopt->flags & DQUOT_QUOTA_SYS_FILE)
  1724. goto put_inodes;
  1725. /* Sync the superblock so that buffers with quota data are written to
  1726. * disk (and so userspace sees correct data afterwards). */
  1727. if (sb->s_op->sync_fs)
  1728. sb->s_op->sync_fs(sb, 1);
  1729. sync_blockdev(sb->s_bdev);
  1730. /* Now the quota files are just ordinary files and we can set the
  1731. * inode flags back. Moreover we discard the pagecache so that
  1732. * userspace sees the writes we did bypassing the pagecache. We
  1733. * must also discard the blockdev buffers so that we see the
  1734. * changes done by userspace on the next quotaon() */
  1735. for (cnt = 0; cnt < MAXQUOTAS; cnt++)
  1736. if (toputinode[cnt]) {
  1737. mutex_lock(&dqopt->dqonoff_mutex);
  1738. /* If quota was reenabled in the meantime, we have
  1739. * nothing to do */
  1740. if (!sb_has_quota_loaded(sb, cnt)) {
  1741. mutex_lock_nested(&toputinode[cnt]->i_mutex,
  1742. I_MUTEX_QUOTA);
  1743. toputinode[cnt]->i_flags &= ~(S_IMMUTABLE |
  1744. S_NOATIME | S_NOQUOTA);
  1745. truncate_inode_pages(&toputinode[cnt]->i_data,
  1746. 0);
  1747. mutex_unlock(&toputinode[cnt]->i_mutex);
  1748. mark_inode_dirty(toputinode[cnt]);
  1749. }
  1750. mutex_unlock(&dqopt->dqonoff_mutex);
  1751. }
  1752. if (sb->s_bdev)
  1753. invalidate_bdev(sb->s_bdev);
  1754. put_inodes:
  1755. for (cnt = 0; cnt < MAXQUOTAS; cnt++)
  1756. if (toputinode[cnt]) {
  1757. /* On remount RO, we keep the inode pointer so that we
  1758. * can reenable quota on the subsequent remount RW. We
  1759. * have to check 'flags' variable and not use sb_has_
  1760. * function because another quotaon / quotaoff could
  1761. * change global state before we got here. We refuse
  1762. * to suspend quotas when there is pending delete on
  1763. * the quota file... */
  1764. if (!(flags & DQUOT_SUSPENDED))
  1765. iput(toputinode[cnt]);
  1766. else if (!toputinode[cnt]->i_nlink)
  1767. ret = -EBUSY;
  1768. }
  1769. return ret;
  1770. }
  1771. EXPORT_SYMBOL(vfs_quota_disable);
  1772. int vfs_quota_off(struct super_block *sb, int type, int remount)
  1773. {
  1774. return vfs_quota_disable(sb, type, remount ? DQUOT_SUSPENDED :
  1775. (DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED));
  1776. }
  1777. EXPORT_SYMBOL(vfs_quota_off);
  1778. /*
  1779. * Turn quotas on on a device
  1780. */
  1781. /*
  1782. * Helper function to turn quotas on when we already have the inode of
  1783. * quota file and no quota information is loaded.
  1784. */
  1785. static int vfs_load_quota_inode(struct inode *inode, int type, int format_id,
  1786. unsigned int flags)
  1787. {
  1788. struct quota_format_type *fmt = find_quota_format(format_id);
  1789. struct super_block *sb = inode->i_sb;
  1790. struct quota_info *dqopt = sb_dqopt(sb);
  1791. int error;
  1792. int oldflags = -1;
  1793. if (!fmt)
  1794. return -ESRCH;
  1795. if (!S_ISREG(inode->i_mode)) {
  1796. error = -EACCES;
  1797. goto out_fmt;
  1798. }
  1799. if (IS_RDONLY(inode)) {
  1800. error = -EROFS;
  1801. goto out_fmt;
  1802. }
  1803. if (!sb->s_op->quota_write || !sb->s_op->quota_read) {
  1804. error = -EINVAL;
  1805. goto out_fmt;
  1806. }
  1807. /* Usage always has to be set... */
  1808. if (!(flags & DQUOT_USAGE_ENABLED)) {
  1809. error = -EINVAL;
  1810. goto out_fmt;
  1811. }
  1812. if (!(dqopt->flags & DQUOT_QUOTA_SYS_FILE)) {
  1813. /* As we bypass the pagecache we must now flush all the
  1814. * dirty data and invalidate caches so that kernel sees
  1815. * changes from userspace. It is not enough to just flush
  1816. * the quota file since if blocksize < pagesize, invalidation
  1817. * of the cache could fail because of other unrelated dirty
  1818. * data */
  1819. sync_filesystem(sb);
  1820. invalidate_bdev(sb->s_bdev);
  1821. }
  1822. mutex_lock(&dqopt->dqonoff_mutex);
  1823. if (sb_has_quota_loaded(sb, type)) {
  1824. error = -EBUSY;
  1825. goto out_lock;
  1826. }
  1827. if (!(dqopt->flags & DQUOT_QUOTA_SYS_FILE)) {
  1828. /* We don't want quota and atime on quota files (deadlocks
  1829. * possible) Also nobody should write to the file - we use
  1830. * special IO operations which ignore the immutable bit. */
  1831. mutex_lock_nested(&inode->i_mutex, I_MUTEX_QUOTA);
  1832. oldflags = inode->i_flags & (S_NOATIME | S_IMMUTABLE |
  1833. S_NOQUOTA);
  1834. inode->i_flags |= S_NOQUOTA | S_NOATIME | S_IMMUTABLE;
  1835. mutex_unlock(&inode->i_mutex);
  1836. /*
  1837. * When S_NOQUOTA is set, remove dquot references as no more
  1838. * references can be added
  1839. */
  1840. sb->dq_op->drop(inode);
  1841. }
  1842. error = -EIO;
  1843. dqopt->files[type] = igrab(inode);
  1844. if (!dqopt->files[type])
  1845. goto out_lock;
  1846. error = -EINVAL;
  1847. if (!fmt->qf_ops->check_quota_file(sb, type))
  1848. goto out_file_init;
  1849. dqopt->ops[type] = fmt->qf_ops;
  1850. dqopt->info[type].dqi_format = fmt;
  1851. dqopt->info[type].dqi_fmt_id = format_id;
  1852. INIT_LIST_HEAD(&dqopt->info[type].dqi_dirty_list);
  1853. mutex_lock(&dqopt->dqio_mutex);
  1854. error = dqopt->ops[type]->read_file_info(sb, type);
  1855. if (error < 0) {
  1856. mutex_unlock(&dqopt->dqio_mutex);
  1857. goto out_file_init;
  1858. }
  1859. mutex_unlock(&dqopt->dqio_mutex);
  1860. spin_lock(&dq_state_lock);
  1861. dqopt->flags |= dquot_state_flag(flags, type);
  1862. spin_unlock(&dq_state_lock);
  1863. add_dquot_ref(sb, type);
  1864. mutex_unlock(&dqopt->dqonoff_mutex);
  1865. return 0;
  1866. out_file_init:
  1867. dqopt->files[type] = NULL;
  1868. iput(inode);
  1869. out_lock:
  1870. if (oldflags != -1) {
  1871. mutex_lock_nested(&inode->i_mutex, I_MUTEX_QUOTA);
  1872. /* Set the flags back (in the case of accidental quotaon()
  1873. * on a wrong file we don't want to mess up the flags) */
  1874. inode->i_flags &= ~(S_NOATIME | S_NOQUOTA | S_IMMUTABLE);
  1875. inode->i_flags |= oldflags;
  1876. mutex_unlock(&inode->i_mutex);
  1877. }
  1878. mutex_unlock(&dqopt->dqonoff_mutex);
  1879. out_fmt:
  1880. put_quota_format(fmt);
  1881. return error;
  1882. }
  1883. /* Reenable quotas on remount RW */
  1884. static int vfs_quota_on_remount(struct super_block *sb, int type)
  1885. {
  1886. struct quota_info *dqopt = sb_dqopt(sb);
  1887. struct inode *inode;
  1888. int ret;
  1889. unsigned int flags;
  1890. mutex_lock(&dqopt->dqonoff_mutex);
  1891. if (!sb_has_quota_suspended(sb, type)) {
  1892. mutex_unlock(&dqopt->dqonoff_mutex);
  1893. return 0;
  1894. }
  1895. inode = dqopt->files[type];
  1896. dqopt->files[type] = NULL;
  1897. spin_lock(&dq_state_lock);
  1898. flags = dqopt->flags & dquot_state_flag(DQUOT_USAGE_ENABLED |
  1899. DQUOT_LIMITS_ENABLED, type);
  1900. dqopt->flags &= ~dquot_state_flag(DQUOT_STATE_FLAGS, type);
  1901. spin_unlock(&dq_state_lock);
  1902. mutex_unlock(&dqopt->dqonoff_mutex);
  1903. flags = dquot_generic_flag(flags, type);
  1904. ret = vfs_load_quota_inode(inode, type, dqopt->info[type].dqi_fmt_id,
  1905. flags);
  1906. iput(inode);
  1907. return ret;
  1908. }
  1909. int vfs_quota_on_path(struct super_block *sb, int type, int format_id,
  1910. struct path *path)
  1911. {
  1912. int error = security_quota_on(path->dentry);
  1913. if (error)
  1914. return error;
  1915. /* Quota file not on the same filesystem? */
  1916. if (path->mnt->mnt_sb != sb)
  1917. error = -EXDEV;
  1918. else
  1919. error = vfs_load_quota_inode(path->dentry->d_inode, type,
  1920. format_id, DQUOT_USAGE_ENABLED |
  1921. DQUOT_LIMITS_ENABLED);
  1922. return error;
  1923. }
  1924. EXPORT_SYMBOL(vfs_quota_on_path);
  1925. int vfs_quota_on(struct super_block *sb, int type, int format_id, char *name,
  1926. int remount)
  1927. {
  1928. struct path path;
  1929. int error;
  1930. if (remount)
  1931. return vfs_quota_on_remount(sb, type);
  1932. error = kern_path(name, LOOKUP_FOLLOW, &path);
  1933. if (!error) {
  1934. error = vfs_quota_on_path(sb, type, format_id, &path);
  1935. path_put(&path);
  1936. }
  1937. return error;
  1938. }
  1939. EXPORT_SYMBOL(vfs_quota_on);
  1940. /*
  1941. * More powerful function for turning on quotas allowing setting
  1942. * of individual quota flags
  1943. */
  1944. int vfs_quota_enable(struct inode *inode, int type, int format_id,
  1945. unsigned int flags)
  1946. {
  1947. int ret = 0;
  1948. struct super_block *sb = inode->i_sb;
  1949. struct quota_info *dqopt = sb_dqopt(sb);
  1950. /* Just unsuspend quotas? */
  1951. if (flags & DQUOT_SUSPENDED)
  1952. return vfs_quota_on_remount(sb, type);
  1953. if (!flags)
  1954. return 0;
  1955. /* Just updating flags needed? */
  1956. if (sb_has_quota_loaded(sb, type)) {
  1957. mutex_lock(&dqopt->dqonoff_mutex);
  1958. /* Now do a reliable test... */
  1959. if (!sb_has_quota_loaded(sb, type)) {
  1960. mutex_unlock(&dqopt->dqonoff_mutex);
  1961. goto load_quota;
  1962. }
  1963. if (flags & DQUOT_USAGE_ENABLED &&
  1964. sb_has_quota_usage_enabled(sb, type)) {
  1965. ret = -EBUSY;
  1966. goto out_lock;
  1967. }
  1968. if (flags & DQUOT_LIMITS_ENABLED &&
  1969. sb_has_quota_limits_enabled(sb, type)) {
  1970. ret = -EBUSY;
  1971. goto out_lock;
  1972. }
  1973. spin_lock(&dq_state_lock);
  1974. sb_dqopt(sb)->flags |= dquot_state_flag(flags, type);
  1975. spin_unlock(&dq_state_lock);
  1976. out_lock:
  1977. mutex_unlock(&dqopt->dqonoff_mutex);
  1978. return ret;
  1979. }
  1980. load_quota:
  1981. return vfs_load_quota_inode(inode, type, format_id, flags);
  1982. }
  1983. EXPORT_SYMBOL(vfs_quota_enable);
  1984. /*
  1985. * This function is used when filesystem needs to initialize quotas
  1986. * during mount time.
  1987. */
  1988. int vfs_quota_on_mount(struct super_block *sb, char *qf_name,
  1989. int format_id, int type)
  1990. {
  1991. struct dentry *dentry;
  1992. int error;
  1993. mutex_lock(&sb->s_root->d_inode->i_mutex);
  1994. dentry = lookup_one_len(qf_name, sb->s_root, strlen(qf_name));
  1995. mutex_unlock(&sb->s_root->d_inode->i_mutex);
  1996. if (IS_ERR(dentry))
  1997. return PTR_ERR(dentry);
  1998. if (!dentry->d_inode) {
  1999. error = -ENOENT;
  2000. goto out;
  2001. }
  2002. error = security_quota_on(dentry);
  2003. if (!error)
  2004. error = vfs_load_quota_inode(dentry->d_inode, type, format_id,
  2005. DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED);
  2006. out:
  2007. dput(dentry);
  2008. return error;
  2009. }
  2010. EXPORT_SYMBOL(vfs_quota_on_mount);
  2011. /* Wrapper to turn on quotas when remounting rw */
  2012. int vfs_dq_quota_on_remount(struct super_block *sb)
  2013. {
  2014. int cnt;
  2015. int ret = 0, err;
  2016. if (!sb->s_qcop || !sb->s_qcop->quota_on)
  2017. return -ENOSYS;
  2018. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  2019. err = sb->s_qcop->quota_on(sb, cnt, 0, NULL, 1);
  2020. if (err < 0 && !ret)
  2021. ret = err;
  2022. }
  2023. return ret;
  2024. }
  2025. EXPORT_SYMBOL(vfs_dq_quota_on_remount);
  2026. static inline qsize_t qbtos(qsize_t blocks)
  2027. {
  2028. return blocks << QIF_DQBLKSIZE_BITS;
  2029. }
  2030. static inline qsize_t stoqb(qsize_t space)
  2031. {
  2032. return (space + QIF_DQBLKSIZE - 1) >> QIF_DQBLKSIZE_BITS;
  2033. }
  2034. /* Generic routine for getting common part of quota structure */
  2035. static void do_get_dqblk(struct dquot *dquot, struct if_dqblk *di)
  2036. {
  2037. struct mem_dqblk *dm = &dquot->dq_dqb;
  2038. spin_lock(&dq_data_lock);
  2039. di->dqb_bhardlimit = stoqb(dm->dqb_bhardlimit);
  2040. di->dqb_bsoftlimit = stoqb(dm->dqb_bsoftlimit);
  2041. di->dqb_curspace = dm->dqb_curspace + dm->dqb_rsvspace;
  2042. di->dqb_ihardlimit = dm->dqb_ihardlimit;
  2043. di->dqb_isoftlimit = dm->dqb_isoftlimit;
  2044. di->dqb_curinodes = dm->dqb_curinodes;
  2045. di->dqb_btime = dm->dqb_btime;
  2046. di->dqb_itime = dm->dqb_itime;
  2047. di->dqb_valid = QIF_ALL;
  2048. spin_unlock(&dq_data_lock);
  2049. }
  2050. int vfs_get_dqblk(struct super_block *sb, int type, qid_t id,
  2051. struct if_dqblk *di)
  2052. {
  2053. struct dquot *dquot;
  2054. dquot = dqget(sb, id, type);
  2055. if (!dquot)
  2056. return -ESRCH;
  2057. do_get_dqblk(dquot, di);
  2058. dqput(dquot);
  2059. return 0;
  2060. }
  2061. EXPORT_SYMBOL(vfs_get_dqblk);
  2062. /* Generic routine for setting common part of quota structure */
  2063. static int do_set_dqblk(struct dquot *dquot, struct if_dqblk *di)
  2064. {
  2065. struct mem_dqblk *dm = &dquot->dq_dqb;
  2066. int check_blim = 0, check_ilim = 0;
  2067. struct mem_dqinfo *dqi = &sb_dqopt(dquot->dq_sb)->info[dquot->dq_type];
  2068. if ((di->dqb_valid & QIF_BLIMITS &&
  2069. (di->dqb_bhardlimit > dqi->dqi_maxblimit ||
  2070. di->dqb_bsoftlimit > dqi->dqi_maxblimit)) ||
  2071. (di->dqb_valid & QIF_ILIMITS &&
  2072. (di->dqb_ihardlimit > dqi->dqi_maxilimit ||
  2073. di->dqb_isoftlimit > dqi->dqi_maxilimit)))
  2074. return -ERANGE;
  2075. spin_lock(&dq_data_lock);
  2076. if (di->dqb_valid & QIF_SPACE) {
  2077. dm->dqb_curspace = di->dqb_curspace - dm->dqb_rsvspace;
  2078. check_blim = 1;
  2079. __set_bit(DQ_LASTSET_B + QIF_SPACE_B, &dquot->dq_flags);
  2080. }
  2081. if (di->dqb_valid & QIF_BLIMITS) {
  2082. dm->dqb_bsoftlimit = qbtos(di->dqb_bsoftlimit);
  2083. dm->dqb_bhardlimit = qbtos(di->dqb_bhardlimit);
  2084. check_blim = 1;
  2085. __set_bit(DQ_LASTSET_B + QIF_BLIMITS_B, &dquot->dq_flags);
  2086. }
  2087. if (di->dqb_valid & QIF_INODES) {
  2088. dm->dqb_curinodes = di->dqb_curinodes;
  2089. check_ilim = 1;
  2090. __set_bit(DQ_LASTSET_B + QIF_INODES_B, &dquot->dq_flags);
  2091. }
  2092. if (di->dqb_valid & QIF_ILIMITS) {
  2093. dm->dqb_isoftlimit = di->dqb_isoftlimit;
  2094. dm->dqb_ihardlimit = di->dqb_ihardlimit;
  2095. check_ilim = 1;
  2096. __set_bit(DQ_LASTSET_B + QIF_ILIMITS_B, &dquot->dq_flags);
  2097. }
  2098. if (di->dqb_valid & QIF_BTIME) {
  2099. dm->dqb_btime = di->dqb_btime;
  2100. check_blim = 1;
  2101. __set_bit(DQ_LASTSET_B + QIF_BTIME_B, &dquot->dq_flags);
  2102. }
  2103. if (di->dqb_valid & QIF_ITIME) {
  2104. dm->dqb_itime = di->dqb_itime;
  2105. check_ilim = 1;
  2106. __set_bit(DQ_LASTSET_B + QIF_ITIME_B, &dquot->dq_flags);
  2107. }
  2108. if (check_blim) {
  2109. if (!dm->dqb_bsoftlimit ||
  2110. dm->dqb_curspace < dm->dqb_bsoftlimit) {
  2111. dm->dqb_btime = 0;
  2112. clear_bit(DQ_BLKS_B, &dquot->dq_flags);
  2113. } else if (!(di->dqb_valid & QIF_BTIME))
  2114. /* Set grace only if user hasn't provided his own... */
  2115. dm->dqb_btime = get_seconds() + dqi->dqi_bgrace;
  2116. }
  2117. if (check_ilim) {
  2118. if (!dm->dqb_isoftlimit ||
  2119. dm->dqb_curinodes < dm->dqb_isoftlimit) {
  2120. dm->dqb_itime = 0;
  2121. clear_bit(DQ_INODES_B, &dquot->dq_flags);
  2122. } else if (!(di->dqb_valid & QIF_ITIME))
  2123. /* Set grace only if user hasn't provided his own... */
  2124. dm->dqb_itime = get_seconds() + dqi->dqi_igrace;
  2125. }
  2126. if (dm->dqb_bhardlimit || dm->dqb_bsoftlimit || dm->dqb_ihardlimit ||
  2127. dm->dqb_isoftlimit)
  2128. clear_bit(DQ_FAKE_B, &dquot->dq_flags);
  2129. else
  2130. set_bit(DQ_FAKE_B, &dquot->dq_flags);
  2131. spin_unlock(&dq_data_lock);
  2132. mark_dquot_dirty(dquot);
  2133. return 0;
  2134. }
  2135. int vfs_set_dqblk(struct super_block *sb, int type, qid_t id,
  2136. struct if_dqblk *di)
  2137. {
  2138. struct dquot *dquot;
  2139. int rc;
  2140. dquot = dqget(sb, id, type);
  2141. if (!dquot) {
  2142. rc = -ESRCH;
  2143. goto out;
  2144. }
  2145. rc = do_set_dqblk(dquot, di);
  2146. dqput(dquot);
  2147. out:
  2148. return rc;
  2149. }
  2150. EXPORT_SYMBOL(vfs_set_dqblk);
  2151. /* Generic routine for getting common part of quota file information */
  2152. int vfs_get_dqinfo(struct super_block *sb, int type, struct if_dqinfo *ii)
  2153. {
  2154. struct mem_dqinfo *mi;
  2155. mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
  2156. if (!sb_has_quota_active(sb, type)) {
  2157. mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
  2158. return -ESRCH;
  2159. }
  2160. mi = sb_dqopt(sb)->info + type;
  2161. spin_lock(&dq_data_lock);
  2162. ii->dqi_bgrace = mi->dqi_bgrace;
  2163. ii->dqi_igrace = mi->dqi_igrace;
  2164. ii->dqi_flags = mi->dqi_flags & DQF_MASK;
  2165. ii->dqi_valid = IIF_ALL;
  2166. spin_unlock(&dq_data_lock);
  2167. mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
  2168. return 0;
  2169. }
  2170. EXPORT_SYMBOL(vfs_get_dqinfo);
  2171. /* Generic routine for setting common part of quota file information */
  2172. int vfs_set_dqinfo(struct super_block *sb, int type, struct if_dqinfo *ii)
  2173. {
  2174. struct mem_dqinfo *mi;
  2175. int err = 0;
  2176. mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
  2177. if (!sb_has_quota_active(sb, type)) {
  2178. err = -ESRCH;
  2179. goto out;
  2180. }
  2181. mi = sb_dqopt(sb)->info + type;
  2182. spin_lock(&dq_data_lock);
  2183. if (ii->dqi_valid & IIF_BGRACE)
  2184. mi->dqi_bgrace = ii->dqi_bgrace;
  2185. if (ii->dqi_valid & IIF_IGRACE)
  2186. mi->dqi_igrace = ii->dqi_igrace;
  2187. if (ii->dqi_valid & IIF_FLAGS)
  2188. mi->dqi_flags = (mi->dqi_flags & ~DQF_MASK) |
  2189. (ii->dqi_flags & DQF_MASK);
  2190. spin_unlock(&dq_data_lock);
  2191. mark_info_dirty(sb, type);
  2192. /* Force write to disk */
  2193. sb->dq_op->write_info(sb, type);
  2194. out:
  2195. mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
  2196. return err;
  2197. }
  2198. EXPORT_SYMBOL(vfs_set_dqinfo);
  2199. const struct quotactl_ops vfs_quotactl_ops = {
  2200. .quota_on = vfs_quota_on,
  2201. .quota_off = vfs_quota_off,
  2202. .quota_sync = vfs_quota_sync,
  2203. .get_info = vfs_get_dqinfo,
  2204. .set_info = vfs_set_dqinfo,
  2205. .get_dqblk = vfs_get_dqblk,
  2206. .set_dqblk = vfs_set_dqblk
  2207. };
  2208. static ctl_table fs_dqstats_table[] = {
  2209. {
  2210. .procname = "lookups",
  2211. .data = &dqstats.lookups,
  2212. .maxlen = sizeof(int),
  2213. .mode = 0444,
  2214. .proc_handler = proc_dointvec,
  2215. },
  2216. {
  2217. .procname = "drops",
  2218. .data = &dqstats.drops,
  2219. .maxlen = sizeof(int),
  2220. .mode = 0444,
  2221. .proc_handler = proc_dointvec,
  2222. },
  2223. {
  2224. .procname = "reads",
  2225. .data = &dqstats.reads,
  2226. .maxlen = sizeof(int),
  2227. .mode = 0444,
  2228. .proc_handler = proc_dointvec,
  2229. },
  2230. {
  2231. .procname = "writes",
  2232. .data = &dqstats.writes,
  2233. .maxlen = sizeof(int),
  2234. .mode = 0444,
  2235. .proc_handler = proc_dointvec,
  2236. },
  2237. {
  2238. .procname = "cache_hits",
  2239. .data = &dqstats.cache_hits,
  2240. .maxlen = sizeof(int),
  2241. .mode = 0444,
  2242. .proc_handler = proc_dointvec,
  2243. },
  2244. {
  2245. .procname = "allocated_dquots",
  2246. .data = &dqstats.allocated_dquots,
  2247. .maxlen = sizeof(int),
  2248. .mode = 0444,
  2249. .proc_handler = proc_dointvec,
  2250. },
  2251. {
  2252. .procname = "free_dquots",
  2253. .data = &dqstats.free_dquots,
  2254. .maxlen = sizeof(int),
  2255. .mode = 0444,
  2256. .proc_handler = proc_dointvec,
  2257. },
  2258. {
  2259. .procname = "syncs",
  2260. .data = &dqstats.syncs,
  2261. .maxlen = sizeof(int),
  2262. .mode = 0444,
  2263. .proc_handler = proc_dointvec,
  2264. },
  2265. #ifdef CONFIG_PRINT_QUOTA_WARNING
  2266. {
  2267. .procname = "warnings",
  2268. .data = &flag_print_warnings,
  2269. .maxlen = sizeof(int),
  2270. .mode = 0644,
  2271. .proc_handler = proc_dointvec,
  2272. },
  2273. #endif
  2274. { },
  2275. };
  2276. static ctl_table fs_table[] = {
  2277. {
  2278. .procname = "quota",
  2279. .mode = 0555,
  2280. .child = fs_dqstats_table,
  2281. },
  2282. { },
  2283. };
  2284. static ctl_table sys_table[] = {
  2285. {
  2286. .procname = "fs",
  2287. .mode = 0555,
  2288. .child = fs_table,
  2289. },
  2290. { },
  2291. };
  2292. static int __init dquot_init(void)
  2293. {
  2294. int i;
  2295. unsigned long nr_hash, order;
  2296. printk(KERN_NOTICE "VFS: Disk quotas %s\n", __DQUOT_VERSION__);
  2297. register_sysctl_table(sys_table);
  2298. dquot_cachep = kmem_cache_create("dquot",
  2299. sizeof(struct dquot), sizeof(unsigned long) * 4,
  2300. (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
  2301. SLAB_MEM_SPREAD|SLAB_PANIC),
  2302. NULL);
  2303. order = 0;
  2304. dquot_hash = (struct hlist_head *)__get_free_pages(GFP_ATOMIC, order);
  2305. if (!dquot_hash)
  2306. panic("Cannot create dquot hash table");
  2307. /* Find power-of-two hlist_heads which can fit into allocation */
  2308. nr_hash = (1UL << order) * PAGE_SIZE / sizeof(struct hlist_head);
  2309. dq_hash_bits = 0;
  2310. do {
  2311. dq_hash_bits++;
  2312. } while (nr_hash >> dq_hash_bits);
  2313. dq_hash_bits--;
  2314. nr_hash = 1UL << dq_hash_bits;
  2315. dq_hash_mask = nr_hash - 1;
  2316. for (i = 0; i < nr_hash; i++)
  2317. INIT_HLIST_HEAD(dquot_hash + i);
  2318. printk("Dquot-cache hash table entries: %ld (order %ld, %ld bytes)\n",
  2319. nr_hash, order, (PAGE_SIZE << order));
  2320. register_shrinker(&dqcache_shrinker);
  2321. return 0;
  2322. }
  2323. module_init(dquot_init);