dquot.c 67 KB

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