dquot.c 52 KB

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