dquot.c 51 KB

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