dquot.c 55 KB

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