dquot.c 71 KB

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