dquot.c 67 KB

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