dquot.c 71 KB

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