dquot.c 67 KB

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