dquot.c 67 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407
  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. genlmsg_multicast(skb, 0, quota_genl_family.id, GFP_NOFS);
  959. return;
  960. attr_err_out:
  961. printk(KERN_ERR "VFS: Not enough space to compose quota message!\n");
  962. err_out:
  963. kfree_skb(skb);
  964. }
  965. #endif
  966. static inline void flush_warnings(struct dquot * const *dquots, char *warntype)
  967. {
  968. int i;
  969. for (i = 0; i < MAXQUOTAS; i++)
  970. if (dquots[i] != NODQUOT && warntype[i] != QUOTA_NL_NOWARN &&
  971. !warning_issued(dquots[i], warntype[i])) {
  972. #ifdef CONFIG_PRINT_QUOTA_WARNING
  973. print_warning(dquots[i], warntype[i]);
  974. #endif
  975. #ifdef CONFIG_QUOTA_NETLINK_INTERFACE
  976. send_warning(dquots[i], warntype[i]);
  977. #endif
  978. }
  979. }
  980. static inline char ignore_hardlimit(struct dquot *dquot)
  981. {
  982. struct mem_dqinfo *info = &sb_dqopt(dquot->dq_sb)->info[dquot->dq_type];
  983. return capable(CAP_SYS_RESOURCE) &&
  984. (info->dqi_format->qf_fmt_id != QFMT_VFS_OLD || !(info->dqi_flags & V1_DQF_RSQUASH));
  985. }
  986. /* needs dq_data_lock */
  987. static int check_idq(struct dquot *dquot, qsize_t inodes, char *warntype)
  988. {
  989. *warntype = QUOTA_NL_NOWARN;
  990. if (!sb_has_quota_limits_enabled(dquot->dq_sb, dquot->dq_type) ||
  991. test_bit(DQ_FAKE_B, &dquot->dq_flags))
  992. return QUOTA_OK;
  993. if (dquot->dq_dqb.dqb_ihardlimit &&
  994. (dquot->dq_dqb.dqb_curinodes + inodes) > dquot->dq_dqb.dqb_ihardlimit &&
  995. !ignore_hardlimit(dquot)) {
  996. *warntype = QUOTA_NL_IHARDWARN;
  997. return NO_QUOTA;
  998. }
  999. if (dquot->dq_dqb.dqb_isoftlimit &&
  1000. (dquot->dq_dqb.dqb_curinodes + inodes) > dquot->dq_dqb.dqb_isoftlimit &&
  1001. dquot->dq_dqb.dqb_itime && get_seconds() >= dquot->dq_dqb.dqb_itime &&
  1002. !ignore_hardlimit(dquot)) {
  1003. *warntype = QUOTA_NL_ISOFTLONGWARN;
  1004. return NO_QUOTA;
  1005. }
  1006. if (dquot->dq_dqb.dqb_isoftlimit &&
  1007. (dquot->dq_dqb.dqb_curinodes + inodes) > dquot->dq_dqb.dqb_isoftlimit &&
  1008. dquot->dq_dqb.dqb_itime == 0) {
  1009. *warntype = QUOTA_NL_ISOFTWARN;
  1010. dquot->dq_dqb.dqb_itime = get_seconds() + sb_dqopt(dquot->dq_sb)->info[dquot->dq_type].dqi_igrace;
  1011. }
  1012. return QUOTA_OK;
  1013. }
  1014. /* needs dq_data_lock */
  1015. static int check_bdq(struct dquot *dquot, qsize_t space, int prealloc, char *warntype)
  1016. {
  1017. *warntype = QUOTA_NL_NOWARN;
  1018. if (!sb_has_quota_limits_enabled(dquot->dq_sb, dquot->dq_type) ||
  1019. test_bit(DQ_FAKE_B, &dquot->dq_flags))
  1020. return QUOTA_OK;
  1021. if (dquot->dq_dqb.dqb_bhardlimit &&
  1022. dquot->dq_dqb.dqb_curspace + space > dquot->dq_dqb.dqb_bhardlimit &&
  1023. !ignore_hardlimit(dquot)) {
  1024. if (!prealloc)
  1025. *warntype = QUOTA_NL_BHARDWARN;
  1026. return NO_QUOTA;
  1027. }
  1028. if (dquot->dq_dqb.dqb_bsoftlimit &&
  1029. dquot->dq_dqb.dqb_curspace + space > dquot->dq_dqb.dqb_bsoftlimit &&
  1030. dquot->dq_dqb.dqb_btime && get_seconds() >= dquot->dq_dqb.dqb_btime &&
  1031. !ignore_hardlimit(dquot)) {
  1032. if (!prealloc)
  1033. *warntype = QUOTA_NL_BSOFTLONGWARN;
  1034. return NO_QUOTA;
  1035. }
  1036. if (dquot->dq_dqb.dqb_bsoftlimit &&
  1037. dquot->dq_dqb.dqb_curspace + space > dquot->dq_dqb.dqb_bsoftlimit &&
  1038. dquot->dq_dqb.dqb_btime == 0) {
  1039. if (!prealloc) {
  1040. *warntype = QUOTA_NL_BSOFTWARN;
  1041. dquot->dq_dqb.dqb_btime = get_seconds() + sb_dqopt(dquot->dq_sb)->info[dquot->dq_type].dqi_bgrace;
  1042. }
  1043. else
  1044. /*
  1045. * We don't allow preallocation to exceed softlimit so exceeding will
  1046. * be always printed
  1047. */
  1048. return NO_QUOTA;
  1049. }
  1050. return QUOTA_OK;
  1051. }
  1052. static int info_idq_free(struct dquot *dquot, qsize_t inodes)
  1053. {
  1054. if (test_bit(DQ_FAKE_B, &dquot->dq_flags) ||
  1055. dquot->dq_dqb.dqb_curinodes <= dquot->dq_dqb.dqb_isoftlimit ||
  1056. !sb_has_quota_limits_enabled(dquot->dq_sb, dquot->dq_type))
  1057. return QUOTA_NL_NOWARN;
  1058. if (dquot->dq_dqb.dqb_curinodes - inodes <= dquot->dq_dqb.dqb_isoftlimit)
  1059. return QUOTA_NL_ISOFTBELOW;
  1060. if (dquot->dq_dqb.dqb_curinodes >= dquot->dq_dqb.dqb_ihardlimit &&
  1061. dquot->dq_dqb.dqb_curinodes - inodes < dquot->dq_dqb.dqb_ihardlimit)
  1062. return QUOTA_NL_IHARDBELOW;
  1063. return QUOTA_NL_NOWARN;
  1064. }
  1065. static int info_bdq_free(struct dquot *dquot, qsize_t space)
  1066. {
  1067. if (test_bit(DQ_FAKE_B, &dquot->dq_flags) ||
  1068. dquot->dq_dqb.dqb_curspace <= dquot->dq_dqb.dqb_bsoftlimit)
  1069. return QUOTA_NL_NOWARN;
  1070. if (dquot->dq_dqb.dqb_curspace - space <= dquot->dq_dqb.dqb_bsoftlimit)
  1071. return QUOTA_NL_BSOFTBELOW;
  1072. if (dquot->dq_dqb.dqb_curspace >= dquot->dq_dqb.dqb_bhardlimit &&
  1073. dquot->dq_dqb.dqb_curspace - space < dquot->dq_dqb.dqb_bhardlimit)
  1074. return QUOTA_NL_BHARDBELOW;
  1075. return QUOTA_NL_NOWARN;
  1076. }
  1077. /*
  1078. * Initialize quota pointers in inode
  1079. * We do things in a bit complicated way but by that we avoid calling
  1080. * dqget() and thus filesystem callbacks under dqptr_sem.
  1081. */
  1082. int dquot_initialize(struct inode *inode, int type)
  1083. {
  1084. unsigned int id = 0;
  1085. int cnt, ret = 0;
  1086. struct dquot *got[MAXQUOTAS] = { NODQUOT, NODQUOT };
  1087. struct super_block *sb = inode->i_sb;
  1088. /* First test before acquiring mutex - solves deadlocks when we
  1089. * re-enter the quota code and are already holding the mutex */
  1090. if (IS_NOQUOTA(inode))
  1091. return 0;
  1092. /* First get references to structures we might need. */
  1093. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1094. if (type != -1 && cnt != type)
  1095. continue;
  1096. switch (cnt) {
  1097. case USRQUOTA:
  1098. id = inode->i_uid;
  1099. break;
  1100. case GRPQUOTA:
  1101. id = inode->i_gid;
  1102. break;
  1103. }
  1104. got[cnt] = dqget(sb, id, cnt);
  1105. }
  1106. down_write(&sb_dqopt(sb)->dqptr_sem);
  1107. /* Having dqptr_sem we know NOQUOTA flags can't be altered... */
  1108. if (IS_NOQUOTA(inode))
  1109. goto out_err;
  1110. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1111. if (type != -1 && cnt != type)
  1112. continue;
  1113. /* Avoid races with quotaoff() */
  1114. if (!sb_has_quota_active(sb, cnt))
  1115. continue;
  1116. if (inode->i_dquot[cnt] == NODQUOT) {
  1117. inode->i_dquot[cnt] = got[cnt];
  1118. got[cnt] = NODQUOT;
  1119. }
  1120. }
  1121. out_err:
  1122. up_write(&sb_dqopt(sb)->dqptr_sem);
  1123. /* Drop unused references */
  1124. for (cnt = 0; cnt < MAXQUOTAS; cnt++)
  1125. dqput(got[cnt]);
  1126. return ret;
  1127. }
  1128. /*
  1129. * Release all quotas referenced by inode
  1130. */
  1131. int dquot_drop(struct inode *inode)
  1132. {
  1133. int cnt;
  1134. struct dquot *put[MAXQUOTAS];
  1135. down_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1136. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1137. put[cnt] = inode->i_dquot[cnt];
  1138. inode->i_dquot[cnt] = NODQUOT;
  1139. }
  1140. up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1141. for (cnt = 0; cnt < MAXQUOTAS; cnt++)
  1142. dqput(put[cnt]);
  1143. return 0;
  1144. }
  1145. /* Wrapper to remove references to quota structures from inode */
  1146. void vfs_dq_drop(struct inode *inode)
  1147. {
  1148. /* Here we can get arbitrary inode from clear_inode() so we have
  1149. * to be careful. OTOH we don't need locking as quota operations
  1150. * are allowed to change only at mount time */
  1151. if (!IS_NOQUOTA(inode) && inode->i_sb && inode->i_sb->dq_op
  1152. && inode->i_sb->dq_op->drop) {
  1153. int cnt;
  1154. /* Test before calling to rule out calls from proc and such
  1155. * where we are not allowed to block. Note that this is
  1156. * actually reliable test even without the lock - the caller
  1157. * must assure that nobody can come after the DQUOT_DROP and
  1158. * add quota pointers back anyway */
  1159. for (cnt = 0; cnt < MAXQUOTAS; cnt++)
  1160. if (inode->i_dquot[cnt] != NODQUOT)
  1161. break;
  1162. if (cnt < MAXQUOTAS)
  1163. inode->i_sb->dq_op->drop(inode);
  1164. }
  1165. }
  1166. /*
  1167. * Following four functions update i_blocks+i_bytes fields and
  1168. * quota information (together with appropriate checks)
  1169. * NOTE: We absolutely rely on the fact that caller dirties
  1170. * the inode (usually macros in quotaops.h care about this) and
  1171. * holds a handle for the current transaction so that dquot write and
  1172. * inode write go into the same transaction.
  1173. */
  1174. /*
  1175. * This operation can block, but only after everything is updated
  1176. */
  1177. int dquot_alloc_space(struct inode *inode, qsize_t number, int warn)
  1178. {
  1179. int cnt, ret = NO_QUOTA;
  1180. char warntype[MAXQUOTAS];
  1181. /* First test before acquiring mutex - solves deadlocks when we
  1182. * re-enter the quota code and are already holding the mutex */
  1183. if (IS_NOQUOTA(inode)) {
  1184. out_add:
  1185. inode_add_bytes(inode, number);
  1186. return QUOTA_OK;
  1187. }
  1188. for (cnt = 0; cnt < MAXQUOTAS; cnt++)
  1189. warntype[cnt] = QUOTA_NL_NOWARN;
  1190. down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1191. if (IS_NOQUOTA(inode)) { /* Now we can do reliable test... */
  1192. up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1193. goto out_add;
  1194. }
  1195. spin_lock(&dq_data_lock);
  1196. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1197. if (inode->i_dquot[cnt] == NODQUOT)
  1198. continue;
  1199. if (check_bdq(inode->i_dquot[cnt], number, warn, warntype+cnt) == NO_QUOTA)
  1200. goto warn_put_all;
  1201. }
  1202. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1203. if (inode->i_dquot[cnt] == NODQUOT)
  1204. continue;
  1205. dquot_incr_space(inode->i_dquot[cnt], number);
  1206. }
  1207. inode_add_bytes(inode, number);
  1208. ret = QUOTA_OK;
  1209. warn_put_all:
  1210. spin_unlock(&dq_data_lock);
  1211. if (ret == QUOTA_OK)
  1212. /* Dirtify all the dquots - this can block when journalling */
  1213. for (cnt = 0; cnt < MAXQUOTAS; cnt++)
  1214. if (inode->i_dquot[cnt])
  1215. mark_dquot_dirty(inode->i_dquot[cnt]);
  1216. flush_warnings(inode->i_dquot, warntype);
  1217. up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1218. return ret;
  1219. }
  1220. /*
  1221. * This operation can block, but only after everything is updated
  1222. */
  1223. int dquot_alloc_inode(const struct inode *inode, qsize_t number)
  1224. {
  1225. int cnt, ret = NO_QUOTA;
  1226. char warntype[MAXQUOTAS];
  1227. /* First test before acquiring mutex - solves deadlocks when we
  1228. * re-enter the quota code and are already holding the mutex */
  1229. if (IS_NOQUOTA(inode))
  1230. return QUOTA_OK;
  1231. for (cnt = 0; cnt < MAXQUOTAS; cnt++)
  1232. warntype[cnt] = QUOTA_NL_NOWARN;
  1233. down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1234. if (IS_NOQUOTA(inode)) {
  1235. up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1236. return QUOTA_OK;
  1237. }
  1238. spin_lock(&dq_data_lock);
  1239. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1240. if (inode->i_dquot[cnt] == NODQUOT)
  1241. continue;
  1242. if (check_idq(inode->i_dquot[cnt], number, warntype+cnt) == NO_QUOTA)
  1243. goto warn_put_all;
  1244. }
  1245. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1246. if (inode->i_dquot[cnt] == NODQUOT)
  1247. continue;
  1248. dquot_incr_inodes(inode->i_dquot[cnt], number);
  1249. }
  1250. ret = QUOTA_OK;
  1251. warn_put_all:
  1252. spin_unlock(&dq_data_lock);
  1253. if (ret == QUOTA_OK)
  1254. /* Dirtify all the dquots - this can block when journalling */
  1255. for (cnt = 0; cnt < MAXQUOTAS; cnt++)
  1256. if (inode->i_dquot[cnt])
  1257. mark_dquot_dirty(inode->i_dquot[cnt]);
  1258. flush_warnings(inode->i_dquot, warntype);
  1259. up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1260. return ret;
  1261. }
  1262. /*
  1263. * This operation can block, but only after everything is updated
  1264. */
  1265. int dquot_free_space(struct inode *inode, qsize_t number)
  1266. {
  1267. unsigned int cnt;
  1268. char warntype[MAXQUOTAS];
  1269. /* First test before acquiring mutex - solves deadlocks when we
  1270. * re-enter the quota code and are already holding the mutex */
  1271. if (IS_NOQUOTA(inode)) {
  1272. out_sub:
  1273. inode_sub_bytes(inode, number);
  1274. return QUOTA_OK;
  1275. }
  1276. down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1277. /* Now recheck reliably when holding dqptr_sem */
  1278. if (IS_NOQUOTA(inode)) {
  1279. up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1280. goto out_sub;
  1281. }
  1282. spin_lock(&dq_data_lock);
  1283. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1284. if (inode->i_dquot[cnt] == NODQUOT)
  1285. continue;
  1286. warntype[cnt] = info_bdq_free(inode->i_dquot[cnt], number);
  1287. dquot_decr_space(inode->i_dquot[cnt], number);
  1288. }
  1289. inode_sub_bytes(inode, number);
  1290. spin_unlock(&dq_data_lock);
  1291. /* Dirtify all the dquots - this can block when journalling */
  1292. for (cnt = 0; cnt < MAXQUOTAS; cnt++)
  1293. if (inode->i_dquot[cnt])
  1294. mark_dquot_dirty(inode->i_dquot[cnt]);
  1295. flush_warnings(inode->i_dquot, warntype);
  1296. up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1297. return QUOTA_OK;
  1298. }
  1299. /*
  1300. * This operation can block, but only after everything is updated
  1301. */
  1302. int dquot_free_inode(const struct inode *inode, qsize_t number)
  1303. {
  1304. unsigned int cnt;
  1305. char warntype[MAXQUOTAS];
  1306. /* First test before acquiring mutex - solves deadlocks when we
  1307. * re-enter the quota code and are already holding the mutex */
  1308. if (IS_NOQUOTA(inode))
  1309. return QUOTA_OK;
  1310. down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1311. /* Now recheck reliably when holding dqptr_sem */
  1312. if (IS_NOQUOTA(inode)) {
  1313. up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1314. return QUOTA_OK;
  1315. }
  1316. spin_lock(&dq_data_lock);
  1317. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1318. if (inode->i_dquot[cnt] == NODQUOT)
  1319. continue;
  1320. warntype[cnt] = info_idq_free(inode->i_dquot[cnt], number);
  1321. dquot_decr_inodes(inode->i_dquot[cnt], number);
  1322. }
  1323. spin_unlock(&dq_data_lock);
  1324. /* Dirtify all the dquots - this can block when journalling */
  1325. for (cnt = 0; cnt < MAXQUOTAS; cnt++)
  1326. if (inode->i_dquot[cnt])
  1327. mark_dquot_dirty(inode->i_dquot[cnt]);
  1328. flush_warnings(inode->i_dquot, warntype);
  1329. up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1330. return QUOTA_OK;
  1331. }
  1332. /*
  1333. * Transfer the number of inode and blocks from one diskquota to an other.
  1334. *
  1335. * This operation can block, but only after everything is updated
  1336. * A transaction must be started when entering this function.
  1337. */
  1338. int dquot_transfer(struct inode *inode, struct iattr *iattr)
  1339. {
  1340. qsize_t space;
  1341. struct dquot *transfer_from[MAXQUOTAS];
  1342. struct dquot *transfer_to[MAXQUOTAS];
  1343. int cnt, ret = QUOTA_OK;
  1344. int chuid = iattr->ia_valid & ATTR_UID && inode->i_uid != iattr->ia_uid,
  1345. chgid = iattr->ia_valid & ATTR_GID && inode->i_gid != iattr->ia_gid;
  1346. char warntype_to[MAXQUOTAS];
  1347. char warntype_from_inodes[MAXQUOTAS], warntype_from_space[MAXQUOTAS];
  1348. /* First test before acquiring mutex - solves deadlocks when we
  1349. * re-enter the quota code and are already holding the mutex */
  1350. if (IS_NOQUOTA(inode))
  1351. return QUOTA_OK;
  1352. /* Initialize the arrays */
  1353. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1354. transfer_from[cnt] = NODQUOT;
  1355. transfer_to[cnt] = NODQUOT;
  1356. warntype_to[cnt] = QUOTA_NL_NOWARN;
  1357. switch (cnt) {
  1358. case USRQUOTA:
  1359. if (!chuid)
  1360. continue;
  1361. transfer_to[cnt] = dqget(inode->i_sb, iattr->ia_uid, cnt);
  1362. break;
  1363. case GRPQUOTA:
  1364. if (!chgid)
  1365. continue;
  1366. transfer_to[cnt] = dqget(inode->i_sb, iattr->ia_gid, cnt);
  1367. break;
  1368. }
  1369. }
  1370. down_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1371. /* Now recheck reliably when holding dqptr_sem */
  1372. if (IS_NOQUOTA(inode)) { /* File without quota accounting? */
  1373. up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1374. goto put_all;
  1375. }
  1376. spin_lock(&dq_data_lock);
  1377. space = inode_get_bytes(inode);
  1378. /* Build the transfer_from list and check the limits */
  1379. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1380. if (transfer_to[cnt] == NODQUOT)
  1381. continue;
  1382. transfer_from[cnt] = inode->i_dquot[cnt];
  1383. if (check_idq(transfer_to[cnt], 1, warntype_to + cnt) ==
  1384. NO_QUOTA || check_bdq(transfer_to[cnt], space, 0,
  1385. warntype_to + cnt) == NO_QUOTA)
  1386. goto over_quota;
  1387. }
  1388. /*
  1389. * Finally perform the needed transfer from transfer_from to transfer_to
  1390. */
  1391. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1392. /*
  1393. * Skip changes for same uid or gid or for turned off quota-type.
  1394. */
  1395. if (transfer_to[cnt] == NODQUOT)
  1396. continue;
  1397. /* Due to IO error we might not have transfer_from[] structure */
  1398. if (transfer_from[cnt]) {
  1399. warntype_from_inodes[cnt] =
  1400. info_idq_free(transfer_from[cnt], 1);
  1401. warntype_from_space[cnt] =
  1402. info_bdq_free(transfer_from[cnt], space);
  1403. dquot_decr_inodes(transfer_from[cnt], 1);
  1404. dquot_decr_space(transfer_from[cnt], space);
  1405. }
  1406. dquot_incr_inodes(transfer_to[cnt], 1);
  1407. dquot_incr_space(transfer_to[cnt], space);
  1408. inode->i_dquot[cnt] = transfer_to[cnt];
  1409. }
  1410. spin_unlock(&dq_data_lock);
  1411. up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1412. /* Dirtify all the dquots - this can block when journalling */
  1413. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1414. if (transfer_from[cnt])
  1415. mark_dquot_dirty(transfer_from[cnt]);
  1416. if (transfer_to[cnt]) {
  1417. mark_dquot_dirty(transfer_to[cnt]);
  1418. /* The reference we got is transferred to the inode */
  1419. transfer_to[cnt] = NODQUOT;
  1420. }
  1421. }
  1422. warn_put_all:
  1423. flush_warnings(transfer_to, warntype_to);
  1424. flush_warnings(transfer_from, warntype_from_inodes);
  1425. flush_warnings(transfer_from, warntype_from_space);
  1426. put_all:
  1427. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1428. dqput(transfer_from[cnt]);
  1429. dqput(transfer_to[cnt]);
  1430. }
  1431. return ret;
  1432. over_quota:
  1433. spin_unlock(&dq_data_lock);
  1434. up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1435. /* Clear dquot pointers we don't want to dqput() */
  1436. for (cnt = 0; cnt < MAXQUOTAS; cnt++)
  1437. transfer_from[cnt] = NODQUOT;
  1438. ret = NO_QUOTA;
  1439. goto warn_put_all;
  1440. }
  1441. /* Wrapper for transferring ownership of an inode */
  1442. int vfs_dq_transfer(struct inode *inode, struct iattr *iattr)
  1443. {
  1444. if (sb_any_quota_active(inode->i_sb) && !IS_NOQUOTA(inode)) {
  1445. vfs_dq_init(inode);
  1446. if (inode->i_sb->dq_op->transfer(inode, iattr) == NO_QUOTA)
  1447. return 1;
  1448. }
  1449. return 0;
  1450. }
  1451. /*
  1452. * Write info of quota file to disk
  1453. */
  1454. int dquot_commit_info(struct super_block *sb, int type)
  1455. {
  1456. int ret;
  1457. struct quota_info *dqopt = sb_dqopt(sb);
  1458. mutex_lock(&dqopt->dqio_mutex);
  1459. ret = dqopt->ops[type]->write_file_info(sb, type);
  1460. mutex_unlock(&dqopt->dqio_mutex);
  1461. return ret;
  1462. }
  1463. /*
  1464. * Definitions of diskquota operations.
  1465. */
  1466. struct dquot_operations dquot_operations = {
  1467. .initialize = dquot_initialize,
  1468. .drop = dquot_drop,
  1469. .alloc_space = dquot_alloc_space,
  1470. .alloc_inode = dquot_alloc_inode,
  1471. .free_space = dquot_free_space,
  1472. .free_inode = dquot_free_inode,
  1473. .transfer = dquot_transfer,
  1474. .write_dquot = dquot_commit,
  1475. .acquire_dquot = dquot_acquire,
  1476. .release_dquot = dquot_release,
  1477. .mark_dirty = dquot_mark_dquot_dirty,
  1478. .write_info = dquot_commit_info,
  1479. .alloc_dquot = dquot_alloc,
  1480. .destroy_dquot = dquot_destroy,
  1481. };
  1482. /*
  1483. * Turn quota off on a device. type == -1 ==> quotaoff for all types (umount)
  1484. */
  1485. int vfs_quota_disable(struct super_block *sb, int type, unsigned int flags)
  1486. {
  1487. int cnt, ret = 0;
  1488. struct quota_info *dqopt = sb_dqopt(sb);
  1489. struct inode *toputinode[MAXQUOTAS];
  1490. /* Cannot turn off usage accounting without turning off limits, or
  1491. * suspend quotas and simultaneously turn quotas off. */
  1492. if ((flags & DQUOT_USAGE_ENABLED && !(flags & DQUOT_LIMITS_ENABLED))
  1493. || (flags & DQUOT_SUSPENDED && flags & (DQUOT_LIMITS_ENABLED |
  1494. DQUOT_USAGE_ENABLED)))
  1495. return -EINVAL;
  1496. /* We need to serialize quota_off() for device */
  1497. mutex_lock(&dqopt->dqonoff_mutex);
  1498. /*
  1499. * Skip everything if there's nothing to do. We have to do this because
  1500. * sometimes we are called when fill_super() failed and calling
  1501. * sync_fs() in such cases does no good.
  1502. */
  1503. if (!sb_any_quota_loaded(sb)) {
  1504. mutex_unlock(&dqopt->dqonoff_mutex);
  1505. return 0;
  1506. }
  1507. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1508. toputinode[cnt] = NULL;
  1509. if (type != -1 && cnt != type)
  1510. continue;
  1511. if (!sb_has_quota_loaded(sb, cnt))
  1512. continue;
  1513. if (flags & DQUOT_SUSPENDED) {
  1514. spin_lock(&dq_state_lock);
  1515. dqopt->flags |=
  1516. dquot_state_flag(DQUOT_SUSPENDED, cnt);
  1517. spin_unlock(&dq_state_lock);
  1518. } else {
  1519. spin_lock(&dq_state_lock);
  1520. dqopt->flags &= ~dquot_state_flag(flags, cnt);
  1521. /* Turning off suspended quotas? */
  1522. if (!sb_has_quota_loaded(sb, cnt) &&
  1523. sb_has_quota_suspended(sb, cnt)) {
  1524. dqopt->flags &= ~dquot_state_flag(
  1525. DQUOT_SUSPENDED, cnt);
  1526. spin_unlock(&dq_state_lock);
  1527. iput(dqopt->files[cnt]);
  1528. dqopt->files[cnt] = NULL;
  1529. continue;
  1530. }
  1531. spin_unlock(&dq_state_lock);
  1532. }
  1533. /* We still have to keep quota loaded? */
  1534. if (sb_has_quota_loaded(sb, cnt) && !(flags & DQUOT_SUSPENDED))
  1535. continue;
  1536. /* Note: these are blocking operations */
  1537. drop_dquot_ref(sb, cnt);
  1538. invalidate_dquots(sb, cnt);
  1539. /*
  1540. * Now all dquots should be invalidated, all writes done so we should be only
  1541. * users of the info. No locks needed.
  1542. */
  1543. if (info_dirty(&dqopt->info[cnt]))
  1544. sb->dq_op->write_info(sb, cnt);
  1545. if (dqopt->ops[cnt]->free_file_info)
  1546. dqopt->ops[cnt]->free_file_info(sb, cnt);
  1547. put_quota_format(dqopt->info[cnt].dqi_format);
  1548. toputinode[cnt] = dqopt->files[cnt];
  1549. if (!sb_has_quota_loaded(sb, cnt))
  1550. dqopt->files[cnt] = NULL;
  1551. dqopt->info[cnt].dqi_flags = 0;
  1552. dqopt->info[cnt].dqi_igrace = 0;
  1553. dqopt->info[cnt].dqi_bgrace = 0;
  1554. dqopt->ops[cnt] = NULL;
  1555. }
  1556. mutex_unlock(&dqopt->dqonoff_mutex);
  1557. /* Skip syncing and setting flags if quota files are hidden */
  1558. if (dqopt->flags & DQUOT_QUOTA_SYS_FILE)
  1559. goto put_inodes;
  1560. /* Sync the superblock so that buffers with quota data are written to
  1561. * disk (and so userspace sees correct data afterwards). */
  1562. if (sb->s_op->sync_fs)
  1563. sb->s_op->sync_fs(sb, 1);
  1564. sync_blockdev(sb->s_bdev);
  1565. /* Now the quota files are just ordinary files and we can set the
  1566. * inode flags back. Moreover we discard the pagecache so that
  1567. * userspace sees the writes we did bypassing the pagecache. We
  1568. * must also discard the blockdev buffers so that we see the
  1569. * changes done by userspace on the next quotaon() */
  1570. for (cnt = 0; cnt < MAXQUOTAS; cnt++)
  1571. if (toputinode[cnt]) {
  1572. mutex_lock(&dqopt->dqonoff_mutex);
  1573. /* If quota was reenabled in the meantime, we have
  1574. * nothing to do */
  1575. if (!sb_has_quota_loaded(sb, cnt)) {
  1576. mutex_lock_nested(&toputinode[cnt]->i_mutex, I_MUTEX_QUOTA);
  1577. toputinode[cnt]->i_flags &= ~(S_IMMUTABLE |
  1578. S_NOATIME | S_NOQUOTA);
  1579. truncate_inode_pages(&toputinode[cnt]->i_data, 0);
  1580. mutex_unlock(&toputinode[cnt]->i_mutex);
  1581. mark_inode_dirty(toputinode[cnt]);
  1582. }
  1583. mutex_unlock(&dqopt->dqonoff_mutex);
  1584. }
  1585. if (sb->s_bdev)
  1586. invalidate_bdev(sb->s_bdev);
  1587. put_inodes:
  1588. for (cnt = 0; cnt < MAXQUOTAS; cnt++)
  1589. if (toputinode[cnt]) {
  1590. /* On remount RO, we keep the inode pointer so that we
  1591. * can reenable quota on the subsequent remount RW. We
  1592. * have to check 'flags' variable and not use sb_has_
  1593. * function because another quotaon / quotaoff could
  1594. * change global state before we got here. We refuse
  1595. * to suspend quotas when there is pending delete on
  1596. * the quota file... */
  1597. if (!(flags & DQUOT_SUSPENDED))
  1598. iput(toputinode[cnt]);
  1599. else if (!toputinode[cnt]->i_nlink)
  1600. ret = -EBUSY;
  1601. }
  1602. return ret;
  1603. }
  1604. int vfs_quota_off(struct super_block *sb, int type, int remount)
  1605. {
  1606. return vfs_quota_disable(sb, type, remount ? DQUOT_SUSPENDED :
  1607. (DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED));
  1608. }
  1609. /*
  1610. * Turn quotas on on a device
  1611. */
  1612. /*
  1613. * Helper function to turn quotas on when we already have the inode of
  1614. * quota file and no quota information is loaded.
  1615. */
  1616. static int vfs_load_quota_inode(struct inode *inode, int type, int format_id,
  1617. unsigned int flags)
  1618. {
  1619. struct quota_format_type *fmt = find_quota_format(format_id);
  1620. struct super_block *sb = inode->i_sb;
  1621. struct quota_info *dqopt = sb_dqopt(sb);
  1622. int error;
  1623. int oldflags = -1;
  1624. if (!fmt)
  1625. return -ESRCH;
  1626. if (!S_ISREG(inode->i_mode)) {
  1627. error = -EACCES;
  1628. goto out_fmt;
  1629. }
  1630. if (IS_RDONLY(inode)) {
  1631. error = -EROFS;
  1632. goto out_fmt;
  1633. }
  1634. if (!sb->s_op->quota_write || !sb->s_op->quota_read) {
  1635. error = -EINVAL;
  1636. goto out_fmt;
  1637. }
  1638. /* Usage always has to be set... */
  1639. if (!(flags & DQUOT_USAGE_ENABLED)) {
  1640. error = -EINVAL;
  1641. goto out_fmt;
  1642. }
  1643. if (!(dqopt->flags & DQUOT_QUOTA_SYS_FILE)) {
  1644. /* As we bypass the pagecache we must now flush the inode so
  1645. * that we see all the changes from userspace... */
  1646. write_inode_now(inode, 1);
  1647. /* And now flush the block cache so that kernel sees the
  1648. * changes */
  1649. invalidate_bdev(sb->s_bdev);
  1650. }
  1651. mutex_lock(&inode->i_mutex);
  1652. mutex_lock(&dqopt->dqonoff_mutex);
  1653. if (sb_has_quota_loaded(sb, type)) {
  1654. error = -EBUSY;
  1655. goto out_lock;
  1656. }
  1657. if (!(dqopt->flags & DQUOT_QUOTA_SYS_FILE)) {
  1658. /* We don't want quota and atime on quota files (deadlocks
  1659. * possible) Also nobody should write to the file - we use
  1660. * special IO operations which ignore the immutable bit. */
  1661. down_write(&dqopt->dqptr_sem);
  1662. oldflags = inode->i_flags & (S_NOATIME | S_IMMUTABLE | S_NOQUOTA);
  1663. inode->i_flags |= S_NOQUOTA | S_NOATIME | S_IMMUTABLE;
  1664. up_write(&dqopt->dqptr_sem);
  1665. sb->dq_op->drop(inode);
  1666. }
  1667. error = -EIO;
  1668. dqopt->files[type] = igrab(inode);
  1669. if (!dqopt->files[type])
  1670. goto out_lock;
  1671. error = -EINVAL;
  1672. if (!fmt->qf_ops->check_quota_file(sb, type))
  1673. goto out_file_init;
  1674. dqopt->ops[type] = fmt->qf_ops;
  1675. dqopt->info[type].dqi_format = fmt;
  1676. dqopt->info[type].dqi_fmt_id = format_id;
  1677. INIT_LIST_HEAD(&dqopt->info[type].dqi_dirty_list);
  1678. mutex_lock(&dqopt->dqio_mutex);
  1679. if ((error = dqopt->ops[type]->read_file_info(sb, type)) < 0) {
  1680. mutex_unlock(&dqopt->dqio_mutex);
  1681. goto out_file_init;
  1682. }
  1683. mutex_unlock(&dqopt->dqio_mutex);
  1684. mutex_unlock(&inode->i_mutex);
  1685. spin_lock(&dq_state_lock);
  1686. dqopt->flags |= dquot_state_flag(flags, type);
  1687. spin_unlock(&dq_state_lock);
  1688. add_dquot_ref(sb, type);
  1689. mutex_unlock(&dqopt->dqonoff_mutex);
  1690. return 0;
  1691. out_file_init:
  1692. dqopt->files[type] = NULL;
  1693. iput(inode);
  1694. out_lock:
  1695. mutex_unlock(&dqopt->dqonoff_mutex);
  1696. if (oldflags != -1) {
  1697. down_write(&dqopt->dqptr_sem);
  1698. /* Set the flags back (in the case of accidental quotaon()
  1699. * on a wrong file we don't want to mess up the flags) */
  1700. inode->i_flags &= ~(S_NOATIME | S_NOQUOTA | S_IMMUTABLE);
  1701. inode->i_flags |= oldflags;
  1702. up_write(&dqopt->dqptr_sem);
  1703. }
  1704. mutex_unlock(&inode->i_mutex);
  1705. out_fmt:
  1706. put_quota_format(fmt);
  1707. return error;
  1708. }
  1709. /* Reenable quotas on remount RW */
  1710. static int vfs_quota_on_remount(struct super_block *sb, int type)
  1711. {
  1712. struct quota_info *dqopt = sb_dqopt(sb);
  1713. struct inode *inode;
  1714. int ret;
  1715. unsigned int flags;
  1716. mutex_lock(&dqopt->dqonoff_mutex);
  1717. if (!sb_has_quota_suspended(sb, type)) {
  1718. mutex_unlock(&dqopt->dqonoff_mutex);
  1719. return 0;
  1720. }
  1721. inode = dqopt->files[type];
  1722. dqopt->files[type] = NULL;
  1723. spin_lock(&dq_state_lock);
  1724. flags = dqopt->flags & dquot_state_flag(DQUOT_USAGE_ENABLED |
  1725. DQUOT_LIMITS_ENABLED, type);
  1726. dqopt->flags &= ~dquot_state_flag(DQUOT_STATE_FLAGS, type);
  1727. spin_unlock(&dq_state_lock);
  1728. mutex_unlock(&dqopt->dqonoff_mutex);
  1729. flags = dquot_generic_flag(flags, type);
  1730. ret = vfs_load_quota_inode(inode, type, dqopt->info[type].dqi_fmt_id,
  1731. flags);
  1732. iput(inode);
  1733. return ret;
  1734. }
  1735. int vfs_quota_on_path(struct super_block *sb, int type, int format_id,
  1736. struct path *path)
  1737. {
  1738. int error = security_quota_on(path->dentry);
  1739. if (error)
  1740. return error;
  1741. /* Quota file not on the same filesystem? */
  1742. if (path->mnt->mnt_sb != sb)
  1743. error = -EXDEV;
  1744. else
  1745. error = vfs_load_quota_inode(path->dentry->d_inode, type,
  1746. format_id, DQUOT_USAGE_ENABLED |
  1747. DQUOT_LIMITS_ENABLED);
  1748. return error;
  1749. }
  1750. int vfs_quota_on(struct super_block *sb, int type, int format_id, char *name,
  1751. int remount)
  1752. {
  1753. struct path path;
  1754. int error;
  1755. if (remount)
  1756. return vfs_quota_on_remount(sb, type);
  1757. error = kern_path(name, LOOKUP_FOLLOW, &path);
  1758. if (!error) {
  1759. error = vfs_quota_on_path(sb, type, format_id, &path);
  1760. path_put(&path);
  1761. }
  1762. return error;
  1763. }
  1764. /*
  1765. * More powerful function for turning on quotas allowing setting
  1766. * of individual quota flags
  1767. */
  1768. int vfs_quota_enable(struct inode *inode, int type, int format_id,
  1769. unsigned int flags)
  1770. {
  1771. int ret = 0;
  1772. struct super_block *sb = inode->i_sb;
  1773. struct quota_info *dqopt = sb_dqopt(sb);
  1774. /* Just unsuspend quotas? */
  1775. if (flags & DQUOT_SUSPENDED)
  1776. return vfs_quota_on_remount(sb, type);
  1777. if (!flags)
  1778. return 0;
  1779. /* Just updating flags needed? */
  1780. if (sb_has_quota_loaded(sb, type)) {
  1781. mutex_lock(&dqopt->dqonoff_mutex);
  1782. /* Now do a reliable test... */
  1783. if (!sb_has_quota_loaded(sb, type)) {
  1784. mutex_unlock(&dqopt->dqonoff_mutex);
  1785. goto load_quota;
  1786. }
  1787. if (flags & DQUOT_USAGE_ENABLED &&
  1788. sb_has_quota_usage_enabled(sb, type)) {
  1789. ret = -EBUSY;
  1790. goto out_lock;
  1791. }
  1792. if (flags & DQUOT_LIMITS_ENABLED &&
  1793. sb_has_quota_limits_enabled(sb, type)) {
  1794. ret = -EBUSY;
  1795. goto out_lock;
  1796. }
  1797. spin_lock(&dq_state_lock);
  1798. sb_dqopt(sb)->flags |= dquot_state_flag(flags, type);
  1799. spin_unlock(&dq_state_lock);
  1800. out_lock:
  1801. mutex_unlock(&dqopt->dqonoff_mutex);
  1802. return ret;
  1803. }
  1804. load_quota:
  1805. return vfs_load_quota_inode(inode, type, format_id, flags);
  1806. }
  1807. /*
  1808. * This function is used when filesystem needs to initialize quotas
  1809. * during mount time.
  1810. */
  1811. int vfs_quota_on_mount(struct super_block *sb, char *qf_name,
  1812. int format_id, int type)
  1813. {
  1814. struct dentry *dentry;
  1815. int error;
  1816. dentry = lookup_one_len(qf_name, sb->s_root, strlen(qf_name));
  1817. if (IS_ERR(dentry))
  1818. return PTR_ERR(dentry);
  1819. if (!dentry->d_inode) {
  1820. error = -ENOENT;
  1821. goto out;
  1822. }
  1823. error = security_quota_on(dentry);
  1824. if (!error)
  1825. error = vfs_load_quota_inode(dentry->d_inode, type, format_id,
  1826. DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED);
  1827. out:
  1828. dput(dentry);
  1829. return error;
  1830. }
  1831. /* Wrapper to turn on quotas when remounting rw */
  1832. int vfs_dq_quota_on_remount(struct super_block *sb)
  1833. {
  1834. int cnt;
  1835. int ret = 0, err;
  1836. if (!sb->s_qcop || !sb->s_qcop->quota_on)
  1837. return -ENOSYS;
  1838. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1839. err = sb->s_qcop->quota_on(sb, cnt, 0, NULL, 1);
  1840. if (err < 0 && !ret)
  1841. ret = err;
  1842. }
  1843. return ret;
  1844. }
  1845. static inline qsize_t qbtos(qsize_t blocks)
  1846. {
  1847. return blocks << QIF_DQBLKSIZE_BITS;
  1848. }
  1849. static inline qsize_t stoqb(qsize_t space)
  1850. {
  1851. return (space + QIF_DQBLKSIZE - 1) >> QIF_DQBLKSIZE_BITS;
  1852. }
  1853. /* Generic routine for getting common part of quota structure */
  1854. static void do_get_dqblk(struct dquot *dquot, struct if_dqblk *di)
  1855. {
  1856. struct mem_dqblk *dm = &dquot->dq_dqb;
  1857. spin_lock(&dq_data_lock);
  1858. di->dqb_bhardlimit = stoqb(dm->dqb_bhardlimit);
  1859. di->dqb_bsoftlimit = stoqb(dm->dqb_bsoftlimit);
  1860. di->dqb_curspace = dm->dqb_curspace;
  1861. di->dqb_ihardlimit = dm->dqb_ihardlimit;
  1862. di->dqb_isoftlimit = dm->dqb_isoftlimit;
  1863. di->dqb_curinodes = dm->dqb_curinodes;
  1864. di->dqb_btime = dm->dqb_btime;
  1865. di->dqb_itime = dm->dqb_itime;
  1866. di->dqb_valid = QIF_ALL;
  1867. spin_unlock(&dq_data_lock);
  1868. }
  1869. int vfs_get_dqblk(struct super_block *sb, int type, qid_t id, struct if_dqblk *di)
  1870. {
  1871. struct dquot *dquot;
  1872. dquot = dqget(sb, id, type);
  1873. if (dquot == NODQUOT)
  1874. return -ESRCH;
  1875. do_get_dqblk(dquot, di);
  1876. dqput(dquot);
  1877. return 0;
  1878. }
  1879. /* Generic routine for setting common part of quota structure */
  1880. static int do_set_dqblk(struct dquot *dquot, struct if_dqblk *di)
  1881. {
  1882. struct mem_dqblk *dm = &dquot->dq_dqb;
  1883. int check_blim = 0, check_ilim = 0;
  1884. struct mem_dqinfo *dqi = &sb_dqopt(dquot->dq_sb)->info[dquot->dq_type];
  1885. if ((di->dqb_valid & QIF_BLIMITS &&
  1886. (di->dqb_bhardlimit > dqi->dqi_maxblimit ||
  1887. di->dqb_bsoftlimit > dqi->dqi_maxblimit)) ||
  1888. (di->dqb_valid & QIF_ILIMITS &&
  1889. (di->dqb_ihardlimit > dqi->dqi_maxilimit ||
  1890. di->dqb_isoftlimit > dqi->dqi_maxilimit)))
  1891. return -ERANGE;
  1892. spin_lock(&dq_data_lock);
  1893. if (di->dqb_valid & QIF_SPACE) {
  1894. dm->dqb_curspace = di->dqb_curspace;
  1895. check_blim = 1;
  1896. __set_bit(DQ_LASTSET_B + QIF_SPACE_B, &dquot->dq_flags);
  1897. }
  1898. if (di->dqb_valid & QIF_BLIMITS) {
  1899. dm->dqb_bsoftlimit = qbtos(di->dqb_bsoftlimit);
  1900. dm->dqb_bhardlimit = qbtos(di->dqb_bhardlimit);
  1901. check_blim = 1;
  1902. __set_bit(DQ_LASTSET_B + QIF_BLIMITS_B, &dquot->dq_flags);
  1903. }
  1904. if (di->dqb_valid & QIF_INODES) {
  1905. dm->dqb_curinodes = di->dqb_curinodes;
  1906. check_ilim = 1;
  1907. __set_bit(DQ_LASTSET_B + QIF_INODES_B, &dquot->dq_flags);
  1908. }
  1909. if (di->dqb_valid & QIF_ILIMITS) {
  1910. dm->dqb_isoftlimit = di->dqb_isoftlimit;
  1911. dm->dqb_ihardlimit = di->dqb_ihardlimit;
  1912. check_ilim = 1;
  1913. __set_bit(DQ_LASTSET_B + QIF_ILIMITS_B, &dquot->dq_flags);
  1914. }
  1915. if (di->dqb_valid & QIF_BTIME) {
  1916. dm->dqb_btime = di->dqb_btime;
  1917. check_blim = 1;
  1918. __set_bit(DQ_LASTSET_B + QIF_BTIME_B, &dquot->dq_flags);
  1919. }
  1920. if (di->dqb_valid & QIF_ITIME) {
  1921. dm->dqb_itime = di->dqb_itime;
  1922. check_ilim = 1;
  1923. __set_bit(DQ_LASTSET_B + QIF_ITIME_B, &dquot->dq_flags);
  1924. }
  1925. if (check_blim) {
  1926. if (!dm->dqb_bsoftlimit || dm->dqb_curspace < dm->dqb_bsoftlimit) {
  1927. dm->dqb_btime = 0;
  1928. clear_bit(DQ_BLKS_B, &dquot->dq_flags);
  1929. }
  1930. else if (!(di->dqb_valid & QIF_BTIME)) /* Set grace only if user hasn't provided his own... */
  1931. dm->dqb_btime = get_seconds() + dqi->dqi_bgrace;
  1932. }
  1933. if (check_ilim) {
  1934. if (!dm->dqb_isoftlimit || dm->dqb_curinodes < dm->dqb_isoftlimit) {
  1935. dm->dqb_itime = 0;
  1936. clear_bit(DQ_INODES_B, &dquot->dq_flags);
  1937. }
  1938. else if (!(di->dqb_valid & QIF_ITIME)) /* Set grace only if user hasn't provided his own... */
  1939. dm->dqb_itime = get_seconds() + dqi->dqi_igrace;
  1940. }
  1941. if (dm->dqb_bhardlimit || dm->dqb_bsoftlimit || dm->dqb_ihardlimit || dm->dqb_isoftlimit)
  1942. clear_bit(DQ_FAKE_B, &dquot->dq_flags);
  1943. else
  1944. set_bit(DQ_FAKE_B, &dquot->dq_flags);
  1945. spin_unlock(&dq_data_lock);
  1946. mark_dquot_dirty(dquot);
  1947. return 0;
  1948. }
  1949. int vfs_set_dqblk(struct super_block *sb, int type, qid_t id, struct if_dqblk *di)
  1950. {
  1951. struct dquot *dquot;
  1952. int rc;
  1953. dquot = dqget(sb, id, type);
  1954. if (!dquot) {
  1955. rc = -ESRCH;
  1956. goto out;
  1957. }
  1958. rc = do_set_dqblk(dquot, di);
  1959. dqput(dquot);
  1960. out:
  1961. return rc;
  1962. }
  1963. /* Generic routine for getting common part of quota file information */
  1964. int vfs_get_dqinfo(struct super_block *sb, int type, struct if_dqinfo *ii)
  1965. {
  1966. struct mem_dqinfo *mi;
  1967. mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
  1968. if (!sb_has_quota_active(sb, type)) {
  1969. mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
  1970. return -ESRCH;
  1971. }
  1972. mi = sb_dqopt(sb)->info + type;
  1973. spin_lock(&dq_data_lock);
  1974. ii->dqi_bgrace = mi->dqi_bgrace;
  1975. ii->dqi_igrace = mi->dqi_igrace;
  1976. ii->dqi_flags = mi->dqi_flags & DQF_MASK;
  1977. ii->dqi_valid = IIF_ALL;
  1978. spin_unlock(&dq_data_lock);
  1979. mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
  1980. return 0;
  1981. }
  1982. /* Generic routine for setting common part of quota file information */
  1983. int vfs_set_dqinfo(struct super_block *sb, int type, struct if_dqinfo *ii)
  1984. {
  1985. struct mem_dqinfo *mi;
  1986. int err = 0;
  1987. mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
  1988. if (!sb_has_quota_active(sb, type)) {
  1989. err = -ESRCH;
  1990. goto out;
  1991. }
  1992. mi = sb_dqopt(sb)->info + type;
  1993. spin_lock(&dq_data_lock);
  1994. if (ii->dqi_valid & IIF_BGRACE)
  1995. mi->dqi_bgrace = ii->dqi_bgrace;
  1996. if (ii->dqi_valid & IIF_IGRACE)
  1997. mi->dqi_igrace = ii->dqi_igrace;
  1998. if (ii->dqi_valid & IIF_FLAGS)
  1999. mi->dqi_flags = (mi->dqi_flags & ~DQF_MASK) | (ii->dqi_flags & DQF_MASK);
  2000. spin_unlock(&dq_data_lock);
  2001. mark_info_dirty(sb, type);
  2002. /* Force write to disk */
  2003. sb->dq_op->write_info(sb, type);
  2004. out:
  2005. mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
  2006. return err;
  2007. }
  2008. struct quotactl_ops vfs_quotactl_ops = {
  2009. .quota_on = vfs_quota_on,
  2010. .quota_off = vfs_quota_off,
  2011. .quota_sync = vfs_quota_sync,
  2012. .get_info = vfs_get_dqinfo,
  2013. .set_info = vfs_set_dqinfo,
  2014. .get_dqblk = vfs_get_dqblk,
  2015. .set_dqblk = vfs_set_dqblk
  2016. };
  2017. static ctl_table fs_dqstats_table[] = {
  2018. {
  2019. .ctl_name = FS_DQ_LOOKUPS,
  2020. .procname = "lookups",
  2021. .data = &dqstats.lookups,
  2022. .maxlen = sizeof(int),
  2023. .mode = 0444,
  2024. .proc_handler = &proc_dointvec,
  2025. },
  2026. {
  2027. .ctl_name = FS_DQ_DROPS,
  2028. .procname = "drops",
  2029. .data = &dqstats.drops,
  2030. .maxlen = sizeof(int),
  2031. .mode = 0444,
  2032. .proc_handler = &proc_dointvec,
  2033. },
  2034. {
  2035. .ctl_name = FS_DQ_READS,
  2036. .procname = "reads",
  2037. .data = &dqstats.reads,
  2038. .maxlen = sizeof(int),
  2039. .mode = 0444,
  2040. .proc_handler = &proc_dointvec,
  2041. },
  2042. {
  2043. .ctl_name = FS_DQ_WRITES,
  2044. .procname = "writes",
  2045. .data = &dqstats.writes,
  2046. .maxlen = sizeof(int),
  2047. .mode = 0444,
  2048. .proc_handler = &proc_dointvec,
  2049. },
  2050. {
  2051. .ctl_name = FS_DQ_CACHE_HITS,
  2052. .procname = "cache_hits",
  2053. .data = &dqstats.cache_hits,
  2054. .maxlen = sizeof(int),
  2055. .mode = 0444,
  2056. .proc_handler = &proc_dointvec,
  2057. },
  2058. {
  2059. .ctl_name = FS_DQ_ALLOCATED,
  2060. .procname = "allocated_dquots",
  2061. .data = &dqstats.allocated_dquots,
  2062. .maxlen = sizeof(int),
  2063. .mode = 0444,
  2064. .proc_handler = &proc_dointvec,
  2065. },
  2066. {
  2067. .ctl_name = FS_DQ_FREE,
  2068. .procname = "free_dquots",
  2069. .data = &dqstats.free_dquots,
  2070. .maxlen = sizeof(int),
  2071. .mode = 0444,
  2072. .proc_handler = &proc_dointvec,
  2073. },
  2074. {
  2075. .ctl_name = FS_DQ_SYNCS,
  2076. .procname = "syncs",
  2077. .data = &dqstats.syncs,
  2078. .maxlen = sizeof(int),
  2079. .mode = 0444,
  2080. .proc_handler = &proc_dointvec,
  2081. },
  2082. #ifdef CONFIG_PRINT_QUOTA_WARNING
  2083. {
  2084. .ctl_name = FS_DQ_WARNINGS,
  2085. .procname = "warnings",
  2086. .data = &flag_print_warnings,
  2087. .maxlen = sizeof(int),
  2088. .mode = 0644,
  2089. .proc_handler = &proc_dointvec,
  2090. },
  2091. #endif
  2092. { .ctl_name = 0 },
  2093. };
  2094. static ctl_table fs_table[] = {
  2095. {
  2096. .ctl_name = FS_DQSTATS,
  2097. .procname = "quota",
  2098. .mode = 0555,
  2099. .child = fs_dqstats_table,
  2100. },
  2101. { .ctl_name = 0 },
  2102. };
  2103. static ctl_table sys_table[] = {
  2104. {
  2105. .ctl_name = CTL_FS,
  2106. .procname = "fs",
  2107. .mode = 0555,
  2108. .child = fs_table,
  2109. },
  2110. { .ctl_name = 0 },
  2111. };
  2112. static int __init dquot_init(void)
  2113. {
  2114. int i;
  2115. unsigned long nr_hash, order;
  2116. printk(KERN_NOTICE "VFS: Disk quotas %s\n", __DQUOT_VERSION__);
  2117. register_sysctl_table(sys_table);
  2118. dquot_cachep = kmem_cache_create("dquot",
  2119. sizeof(struct dquot), sizeof(unsigned long) * 4,
  2120. (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
  2121. SLAB_MEM_SPREAD|SLAB_PANIC),
  2122. NULL);
  2123. order = 0;
  2124. dquot_hash = (struct hlist_head *)__get_free_pages(GFP_ATOMIC, order);
  2125. if (!dquot_hash)
  2126. panic("Cannot create dquot hash table");
  2127. /* Find power-of-two hlist_heads which can fit into allocation */
  2128. nr_hash = (1UL << order) * PAGE_SIZE / sizeof(struct hlist_head);
  2129. dq_hash_bits = 0;
  2130. do {
  2131. dq_hash_bits++;
  2132. } while (nr_hash >> dq_hash_bits);
  2133. dq_hash_bits--;
  2134. nr_hash = 1UL << dq_hash_bits;
  2135. dq_hash_mask = nr_hash - 1;
  2136. for (i = 0; i < nr_hash; i++)
  2137. INIT_HLIST_HEAD(dquot_hash + i);
  2138. printk("Dquot-cache hash table entries: %ld (order %ld, %ld bytes)\n",
  2139. nr_hash, order, (PAGE_SIZE << order));
  2140. register_shrinker(&dqcache_shrinker);
  2141. #ifdef CONFIG_QUOTA_NETLINK_INTERFACE
  2142. if (genl_register_family(&quota_genl_family) != 0)
  2143. printk(KERN_ERR "VFS: Failed to create quota netlink interface.\n");
  2144. #endif
  2145. return 0;
  2146. }
  2147. module_init(dquot_init);
  2148. EXPORT_SYMBOL(register_quota_format);
  2149. EXPORT_SYMBOL(unregister_quota_format);
  2150. EXPORT_SYMBOL(dqstats);
  2151. EXPORT_SYMBOL(dq_data_lock);
  2152. EXPORT_SYMBOL(vfs_quota_enable);
  2153. EXPORT_SYMBOL(vfs_quota_on);
  2154. EXPORT_SYMBOL(vfs_quota_on_path);
  2155. EXPORT_SYMBOL(vfs_quota_on_mount);
  2156. EXPORT_SYMBOL(vfs_quota_disable);
  2157. EXPORT_SYMBOL(vfs_quota_off);
  2158. EXPORT_SYMBOL(dquot_scan_active);
  2159. EXPORT_SYMBOL(vfs_quota_sync);
  2160. EXPORT_SYMBOL(vfs_get_dqinfo);
  2161. EXPORT_SYMBOL(vfs_set_dqinfo);
  2162. EXPORT_SYMBOL(vfs_get_dqblk);
  2163. EXPORT_SYMBOL(vfs_set_dqblk);
  2164. EXPORT_SYMBOL(dquot_commit);
  2165. EXPORT_SYMBOL(dquot_commit_info);
  2166. EXPORT_SYMBOL(dquot_acquire);
  2167. EXPORT_SYMBOL(dquot_release);
  2168. EXPORT_SYMBOL(dquot_mark_dquot_dirty);
  2169. EXPORT_SYMBOL(dquot_initialize);
  2170. EXPORT_SYMBOL(dquot_drop);
  2171. EXPORT_SYMBOL(vfs_dq_drop);
  2172. EXPORT_SYMBOL(dqget);
  2173. EXPORT_SYMBOL(dqput);
  2174. EXPORT_SYMBOL(dquot_alloc_space);
  2175. EXPORT_SYMBOL(dquot_alloc_inode);
  2176. EXPORT_SYMBOL(dquot_free_space);
  2177. EXPORT_SYMBOL(dquot_free_inode);
  2178. EXPORT_SYMBOL(dquot_transfer);
  2179. EXPORT_SYMBOL(vfs_dq_transfer);
  2180. EXPORT_SYMBOL(vfs_dq_quota_on_remount);