dquot.c 72 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660
  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/capability.h>
  76. #include <linux/quotaops.h>
  77. #include "../internal.h" /* ugh */
  78. #include <asm/uaccess.h>
  79. /*
  80. * There are three quota SMP locks. dq_list_lock protects all lists with quotas
  81. * and quota formats.
  82. * dq_data_lock protects data from dq_dqb and also mem_dqinfo structures and
  83. * also guards consistency of dquot->dq_dqb with inode->i_blocks, i_bytes.
  84. * i_blocks and i_bytes updates itself are guarded by i_lock acquired directly
  85. * in inode_add_bytes() and inode_sub_bytes(). dq_state_lock protects
  86. * modifications of quota state (on quotaon and quotaoff) and readers who care
  87. * about latest values take it as well.
  88. *
  89. * The spinlock ordering is hence: dq_data_lock > dq_list_lock > i_lock,
  90. * dq_list_lock > dq_state_lock
  91. *
  92. * Note that some things (eg. sb pointer, type, id) doesn't change during
  93. * the life of the dquot structure and so needn't to be protected by a lock
  94. *
  95. * Any operation working on dquots via inode pointers must hold dqptr_sem. If
  96. * operation is just reading pointers from inode (or not using them at all) the
  97. * read lock is enough. If pointers are altered function must hold write lock.
  98. * Special care needs to be taken about S_NOQUOTA inode flag (marking that
  99. * inode is a quota file). Functions adding pointers from inode to dquots have
  100. * to check this flag under dqptr_sem and then (if S_NOQUOTA is not set) they
  101. * have to do all pointer modifications before dropping dqptr_sem. This makes
  102. * sure they cannot race with quotaon which first sets S_NOQUOTA flag and
  103. * then drops all pointers to dquots from an inode.
  104. *
  105. * Each dquot has its dq_lock mutex. 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_mutex > dqonoff_sem > journal_lock > dqptr_sem > dquot->dq_lock >
  116. * dqio_mutex
  117. * The lock ordering of dqptr_sem imposed by quota code is only dqonoff_sem >
  118. * dqptr_sem. But filesystem has to count with the fact that functions such as
  119. * dquot_alloc_space() acquire dqptr_sem and they usually have to be called
  120. * from inside a transaction to keep filesystem consistency after a crash. Also
  121. * filesystems usually want to do some IO on dquot from ->mark_dirty which is
  122. * called with dqptr_sem held.
  123. * i_mutex on quota files is special (it's below dqio_mutex)
  124. */
  125. static __cacheline_aligned_in_smp DEFINE_SPINLOCK(dq_list_lock);
  126. static __cacheline_aligned_in_smp DEFINE_SPINLOCK(dq_state_lock);
  127. __cacheline_aligned_in_smp DEFINE_SPINLOCK(dq_data_lock);
  128. EXPORT_SYMBOL(dq_data_lock);
  129. void __quota_error(struct super_block *sb, const char *func,
  130. const char *fmt, ...)
  131. {
  132. if (printk_ratelimit()) {
  133. va_list args;
  134. struct va_format vaf;
  135. va_start(args, fmt);
  136. vaf.fmt = fmt;
  137. vaf.va = &args;
  138. printk(KERN_ERR "Quota error (device %s): %s: %pV\n",
  139. sb->s_id, func, &vaf);
  140. va_end(args);
  141. }
  142. }
  143. EXPORT_SYMBOL(__quota_error);
  144. #if defined(CONFIG_QUOTA_DEBUG) || defined(CONFIG_PRINT_QUOTA_WARNING)
  145. static char *quotatypes[] = INITQFNAMES;
  146. #endif
  147. static struct quota_format_type *quota_formats; /* List of registered formats */
  148. static struct quota_module_name module_names[] = INIT_QUOTA_MODULE_NAMES;
  149. /* SLAB cache for dquot structures */
  150. static struct kmem_cache *dquot_cachep;
  151. int register_quota_format(struct quota_format_type *fmt)
  152. {
  153. spin_lock(&dq_list_lock);
  154. fmt->qf_next = quota_formats;
  155. quota_formats = fmt;
  156. spin_unlock(&dq_list_lock);
  157. return 0;
  158. }
  159. EXPORT_SYMBOL(register_quota_format);
  160. void unregister_quota_format(struct quota_format_type *fmt)
  161. {
  162. struct quota_format_type **actqf;
  163. spin_lock(&dq_list_lock);
  164. for (actqf = &quota_formats; *actqf && *actqf != fmt;
  165. actqf = &(*actqf)->qf_next)
  166. ;
  167. if (*actqf)
  168. *actqf = (*actqf)->qf_next;
  169. spin_unlock(&dq_list_lock);
  170. }
  171. EXPORT_SYMBOL(unregister_quota_format);
  172. static struct quota_format_type *find_quota_format(int id)
  173. {
  174. struct quota_format_type *actqf;
  175. spin_lock(&dq_list_lock);
  176. for (actqf = quota_formats; actqf && actqf->qf_fmt_id != id;
  177. actqf = actqf->qf_next)
  178. ;
  179. if (!actqf || !try_module_get(actqf->qf_owner)) {
  180. int qm;
  181. spin_unlock(&dq_list_lock);
  182. for (qm = 0; module_names[qm].qm_fmt_id &&
  183. module_names[qm].qm_fmt_id != id; qm++)
  184. ;
  185. if (!module_names[qm].qm_fmt_id ||
  186. request_module(module_names[qm].qm_mod_name))
  187. return NULL;
  188. spin_lock(&dq_list_lock);
  189. for (actqf = quota_formats; actqf && actqf->qf_fmt_id != id;
  190. actqf = actqf->qf_next)
  191. ;
  192. if (actqf && !try_module_get(actqf->qf_owner))
  193. actqf = NULL;
  194. }
  195. spin_unlock(&dq_list_lock);
  196. return actqf;
  197. }
  198. static void put_quota_format(struct quota_format_type *fmt)
  199. {
  200. module_put(fmt->qf_owner);
  201. }
  202. /*
  203. * Dquot List Management:
  204. * The quota code uses three lists for dquot management: the inuse_list,
  205. * free_dquots, and dquot_hash[] array. A single dquot structure may be
  206. * on all three lists, depending on its current state.
  207. *
  208. * All dquots are placed to the end of inuse_list when first created, and this
  209. * list is used for invalidate operation, which must look at every dquot.
  210. *
  211. * Unused dquots (dq_count == 0) are added to the free_dquots list when freed,
  212. * and this list is searched whenever we need an available dquot. Dquots are
  213. * removed from the list as soon as they are used again, and
  214. * dqstats.free_dquots gives the number of dquots on the list. When
  215. * dquot is invalidated it's completely released from memory.
  216. *
  217. * Dquots with a specific identity (device, type and id) are placed on
  218. * one of the dquot_hash[] hash chains. The provides an efficient search
  219. * mechanism to locate a specific dquot.
  220. */
  221. static LIST_HEAD(inuse_list);
  222. static LIST_HEAD(free_dquots);
  223. static unsigned int dq_hash_bits, dq_hash_mask;
  224. static struct hlist_head *dquot_hash;
  225. struct dqstats dqstats;
  226. EXPORT_SYMBOL(dqstats);
  227. static qsize_t inode_get_rsv_space(struct inode *inode);
  228. static void __dquot_initialize(struct inode *inode, int type);
  229. static inline unsigned int
  230. hashfn(const struct super_block *sb, unsigned int id, int type)
  231. {
  232. unsigned long tmp;
  233. tmp = (((unsigned long)sb>>L1_CACHE_SHIFT) ^ id) * (MAXQUOTAS - type);
  234. return (tmp + (tmp >> dq_hash_bits)) & dq_hash_mask;
  235. }
  236. /*
  237. * Following list functions expect dq_list_lock to be held
  238. */
  239. static inline void insert_dquot_hash(struct dquot *dquot)
  240. {
  241. struct hlist_head *head;
  242. head = dquot_hash + hashfn(dquot->dq_sb, dquot->dq_id, dquot->dq_type);
  243. hlist_add_head(&dquot->dq_hash, head);
  244. }
  245. static inline void remove_dquot_hash(struct dquot *dquot)
  246. {
  247. hlist_del_init(&dquot->dq_hash);
  248. }
  249. static struct dquot *find_dquot(unsigned int hashent, struct super_block *sb,
  250. unsigned int id, int type)
  251. {
  252. struct hlist_node *node;
  253. struct dquot *dquot;
  254. hlist_for_each (node, dquot_hash+hashent) {
  255. dquot = hlist_entry(node, struct dquot, dq_hash);
  256. if (dquot->dq_sb == sb && dquot->dq_id == id &&
  257. dquot->dq_type == type)
  258. return dquot;
  259. }
  260. return NULL;
  261. }
  262. /* Add a dquot to the tail of the free list */
  263. static inline void put_dquot_last(struct dquot *dquot)
  264. {
  265. list_add_tail(&dquot->dq_free, &free_dquots);
  266. dqstats_inc(DQST_FREE_DQUOTS);
  267. }
  268. static inline void remove_free_dquot(struct dquot *dquot)
  269. {
  270. if (list_empty(&dquot->dq_free))
  271. return;
  272. list_del_init(&dquot->dq_free);
  273. dqstats_dec(DQST_FREE_DQUOTS);
  274. }
  275. static inline void put_inuse(struct dquot *dquot)
  276. {
  277. /* We add to the back of inuse list so we don't have to restart
  278. * when traversing this list and we block */
  279. list_add_tail(&dquot->dq_inuse, &inuse_list);
  280. dqstats_inc(DQST_ALLOC_DQUOTS);
  281. }
  282. static inline void remove_inuse(struct dquot *dquot)
  283. {
  284. dqstats_dec(DQST_ALLOC_DQUOTS);
  285. list_del(&dquot->dq_inuse);
  286. }
  287. /*
  288. * End of list functions needing dq_list_lock
  289. */
  290. static void wait_on_dquot(struct dquot *dquot)
  291. {
  292. mutex_lock(&dquot->dq_lock);
  293. mutex_unlock(&dquot->dq_lock);
  294. }
  295. static inline int dquot_dirty(struct dquot *dquot)
  296. {
  297. return test_bit(DQ_MOD_B, &dquot->dq_flags);
  298. }
  299. static inline int mark_dquot_dirty(struct dquot *dquot)
  300. {
  301. return dquot->dq_sb->dq_op->mark_dirty(dquot);
  302. }
  303. /* Mark dquot dirty in atomic manner, and return it's old dirty flag state */
  304. int dquot_mark_dquot_dirty(struct dquot *dquot)
  305. {
  306. int ret = 1;
  307. /* If quota is dirty already, we don't have to acquire dq_list_lock */
  308. if (test_bit(DQ_MOD_B, &dquot->dq_flags))
  309. return 1;
  310. spin_lock(&dq_list_lock);
  311. if (!test_and_set_bit(DQ_MOD_B, &dquot->dq_flags)) {
  312. list_add(&dquot->dq_dirty, &sb_dqopt(dquot->dq_sb)->
  313. info[dquot->dq_type].dqi_dirty_list);
  314. ret = 0;
  315. }
  316. spin_unlock(&dq_list_lock);
  317. return ret;
  318. }
  319. EXPORT_SYMBOL(dquot_mark_dquot_dirty);
  320. /* Dirtify all the dquots - this can block when journalling */
  321. static inline int mark_all_dquot_dirty(struct dquot * const *dquot)
  322. {
  323. int ret, err, cnt;
  324. ret = err = 0;
  325. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  326. if (dquot[cnt])
  327. /* Even in case of error we have to continue */
  328. ret = mark_dquot_dirty(dquot[cnt]);
  329. if (!err)
  330. err = ret;
  331. }
  332. return err;
  333. }
  334. static inline void dqput_all(struct dquot **dquot)
  335. {
  336. unsigned int cnt;
  337. for (cnt = 0; cnt < MAXQUOTAS; cnt++)
  338. dqput(dquot[cnt]);
  339. }
  340. /* This function needs dq_list_lock */
  341. static inline int clear_dquot_dirty(struct dquot *dquot)
  342. {
  343. if (!test_and_clear_bit(DQ_MOD_B, &dquot->dq_flags))
  344. return 0;
  345. list_del_init(&dquot->dq_dirty);
  346. return 1;
  347. }
  348. void mark_info_dirty(struct super_block *sb, int type)
  349. {
  350. set_bit(DQF_INFO_DIRTY_B, &sb_dqopt(sb)->info[type].dqi_flags);
  351. }
  352. EXPORT_SYMBOL(mark_info_dirty);
  353. /*
  354. * Read dquot from disk and alloc space for it
  355. */
  356. int dquot_acquire(struct dquot *dquot)
  357. {
  358. int ret = 0, ret2 = 0;
  359. struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
  360. mutex_lock(&dquot->dq_lock);
  361. mutex_lock(&dqopt->dqio_mutex);
  362. if (!test_bit(DQ_READ_B, &dquot->dq_flags))
  363. ret = dqopt->ops[dquot->dq_type]->read_dqblk(dquot);
  364. if (ret < 0)
  365. goto out_iolock;
  366. set_bit(DQ_READ_B, &dquot->dq_flags);
  367. /* Instantiate dquot if needed */
  368. if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags) && !dquot->dq_off) {
  369. ret = dqopt->ops[dquot->dq_type]->commit_dqblk(dquot);
  370. /* Write the info if needed */
  371. if (info_dirty(&dqopt->info[dquot->dq_type])) {
  372. ret2 = dqopt->ops[dquot->dq_type]->write_file_info(
  373. dquot->dq_sb, dquot->dq_type);
  374. }
  375. if (ret < 0)
  376. goto out_iolock;
  377. if (ret2 < 0) {
  378. ret = ret2;
  379. goto out_iolock;
  380. }
  381. }
  382. set_bit(DQ_ACTIVE_B, &dquot->dq_flags);
  383. out_iolock:
  384. mutex_unlock(&dqopt->dqio_mutex);
  385. mutex_unlock(&dquot->dq_lock);
  386. return ret;
  387. }
  388. EXPORT_SYMBOL(dquot_acquire);
  389. /*
  390. * Write dquot to disk
  391. */
  392. int dquot_commit(struct dquot *dquot)
  393. {
  394. int ret = 0;
  395. struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
  396. mutex_lock(&dqopt->dqio_mutex);
  397. spin_lock(&dq_list_lock);
  398. if (!clear_dquot_dirty(dquot)) {
  399. spin_unlock(&dq_list_lock);
  400. goto out_sem;
  401. }
  402. spin_unlock(&dq_list_lock);
  403. /* Inactive dquot can be only if there was error during read/init
  404. * => we have better not writing it */
  405. if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags))
  406. ret = dqopt->ops[dquot->dq_type]->commit_dqblk(dquot);
  407. else
  408. ret = -EIO;
  409. out_sem:
  410. mutex_unlock(&dqopt->dqio_mutex);
  411. return ret;
  412. }
  413. EXPORT_SYMBOL(dquot_commit);
  414. /*
  415. * Release dquot
  416. */
  417. int dquot_release(struct dquot *dquot)
  418. {
  419. int ret = 0, ret2 = 0;
  420. struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
  421. mutex_lock(&dquot->dq_lock);
  422. /* Check whether we are not racing with some other dqget() */
  423. if (atomic_read(&dquot->dq_count) > 1)
  424. goto out_dqlock;
  425. mutex_lock(&dqopt->dqio_mutex);
  426. if (dqopt->ops[dquot->dq_type]->release_dqblk) {
  427. ret = dqopt->ops[dquot->dq_type]->release_dqblk(dquot);
  428. /* Write the info */
  429. if (info_dirty(&dqopt->info[dquot->dq_type])) {
  430. ret2 = dqopt->ops[dquot->dq_type]->write_file_info(
  431. dquot->dq_sb, dquot->dq_type);
  432. }
  433. if (ret >= 0)
  434. ret = ret2;
  435. }
  436. clear_bit(DQ_ACTIVE_B, &dquot->dq_flags);
  437. mutex_unlock(&dqopt->dqio_mutex);
  438. out_dqlock:
  439. mutex_unlock(&dquot->dq_lock);
  440. return ret;
  441. }
  442. EXPORT_SYMBOL(dquot_release);
  443. void dquot_destroy(struct dquot *dquot)
  444. {
  445. kmem_cache_free(dquot_cachep, dquot);
  446. }
  447. EXPORT_SYMBOL(dquot_destroy);
  448. static inline void do_destroy_dquot(struct dquot *dquot)
  449. {
  450. dquot->dq_sb->dq_op->destroy_dquot(dquot);
  451. }
  452. /* Invalidate all dquots on the list. Note that this function is called after
  453. * quota is disabled and pointers from inodes removed so there cannot be new
  454. * quota users. There can still be some users of quotas due to inodes being
  455. * just deleted or pruned by prune_icache() (those are not attached to any
  456. * list) or parallel quotactl call. We have to wait for such users.
  457. */
  458. static void invalidate_dquots(struct super_block *sb, int type)
  459. {
  460. struct dquot *dquot, *tmp;
  461. restart:
  462. spin_lock(&dq_list_lock);
  463. list_for_each_entry_safe(dquot, tmp, &inuse_list, dq_inuse) {
  464. if (dquot->dq_sb != sb)
  465. continue;
  466. if (dquot->dq_type != type)
  467. continue;
  468. /* Wait for dquot users */
  469. if (atomic_read(&dquot->dq_count)) {
  470. DEFINE_WAIT(wait);
  471. atomic_inc(&dquot->dq_count);
  472. prepare_to_wait(&dquot->dq_wait_unused, &wait,
  473. TASK_UNINTERRUPTIBLE);
  474. spin_unlock(&dq_list_lock);
  475. /* Once dqput() wakes us up, we know it's time to free
  476. * the dquot.
  477. * IMPORTANT: we rely on the fact that there is always
  478. * at most one process waiting for dquot to free.
  479. * Otherwise dq_count would be > 1 and we would never
  480. * wake up.
  481. */
  482. if (atomic_read(&dquot->dq_count) > 1)
  483. schedule();
  484. finish_wait(&dquot->dq_wait_unused, &wait);
  485. dqput(dquot);
  486. /* At this moment dquot() need not exist (it could be
  487. * reclaimed by prune_dqcache(). Hence we must
  488. * restart. */
  489. goto restart;
  490. }
  491. /*
  492. * Quota now has no users and it has been written on last
  493. * dqput()
  494. */
  495. remove_dquot_hash(dquot);
  496. remove_free_dquot(dquot);
  497. remove_inuse(dquot);
  498. do_destroy_dquot(dquot);
  499. }
  500. spin_unlock(&dq_list_lock);
  501. }
  502. /* Call callback for every active dquot on given filesystem */
  503. int dquot_scan_active(struct super_block *sb,
  504. int (*fn)(struct dquot *dquot, unsigned long priv),
  505. unsigned long priv)
  506. {
  507. struct dquot *dquot, *old_dquot = NULL;
  508. int ret = 0;
  509. mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
  510. spin_lock(&dq_list_lock);
  511. list_for_each_entry(dquot, &inuse_list, dq_inuse) {
  512. if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags))
  513. continue;
  514. if (dquot->dq_sb != sb)
  515. continue;
  516. /* Now we have active dquot so we can just increase use count */
  517. atomic_inc(&dquot->dq_count);
  518. spin_unlock(&dq_list_lock);
  519. dqstats_inc(DQST_LOOKUPS);
  520. dqput(old_dquot);
  521. old_dquot = dquot;
  522. ret = fn(dquot, priv);
  523. if (ret < 0)
  524. goto out;
  525. spin_lock(&dq_list_lock);
  526. /* We are safe to continue now because our dquot could not
  527. * be moved out of the inuse list while we hold the reference */
  528. }
  529. spin_unlock(&dq_list_lock);
  530. out:
  531. dqput(old_dquot);
  532. mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
  533. return ret;
  534. }
  535. EXPORT_SYMBOL(dquot_scan_active);
  536. int dquot_quota_sync(struct super_block *sb, int type, int wait)
  537. {
  538. struct list_head *dirty;
  539. struct dquot *dquot;
  540. struct quota_info *dqopt = sb_dqopt(sb);
  541. int cnt;
  542. mutex_lock(&dqopt->dqonoff_mutex);
  543. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  544. if (type != -1 && cnt != type)
  545. continue;
  546. if (!sb_has_quota_active(sb, cnt))
  547. continue;
  548. spin_lock(&dq_list_lock);
  549. dirty = &dqopt->info[cnt].dqi_dirty_list;
  550. while (!list_empty(dirty)) {
  551. dquot = list_first_entry(dirty, struct dquot,
  552. dq_dirty);
  553. /* Dirty and inactive can be only bad dquot... */
  554. if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
  555. clear_dquot_dirty(dquot);
  556. continue;
  557. }
  558. /* Now we have active dquot from which someone is
  559. * holding reference so we can safely just increase
  560. * use count */
  561. atomic_inc(&dquot->dq_count);
  562. spin_unlock(&dq_list_lock);
  563. dqstats_inc(DQST_LOOKUPS);
  564. sb->dq_op->write_dquot(dquot);
  565. dqput(dquot);
  566. spin_lock(&dq_list_lock);
  567. }
  568. spin_unlock(&dq_list_lock);
  569. }
  570. for (cnt = 0; cnt < MAXQUOTAS; cnt++)
  571. if ((cnt == type || type == -1) && sb_has_quota_active(sb, cnt)
  572. && info_dirty(&dqopt->info[cnt]))
  573. sb->dq_op->write_info(sb, cnt);
  574. dqstats_inc(DQST_SYNCS);
  575. mutex_unlock(&dqopt->dqonoff_mutex);
  576. if (!wait || (sb_dqopt(sb)->flags & DQUOT_QUOTA_SYS_FILE))
  577. return 0;
  578. /* This is not very clever (and fast) but currently I don't know about
  579. * any other simple way of getting quota data to disk and we must get
  580. * them there for userspace to be visible... */
  581. if (sb->s_op->sync_fs)
  582. sb->s_op->sync_fs(sb, 1);
  583. sync_blockdev(sb->s_bdev);
  584. /*
  585. * Now when everything is written we can discard the pagecache so
  586. * that userspace sees the changes.
  587. */
  588. mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
  589. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  590. if (type != -1 && cnt != type)
  591. continue;
  592. if (!sb_has_quota_active(sb, cnt))
  593. continue;
  594. mutex_lock_nested(&sb_dqopt(sb)->files[cnt]->i_mutex,
  595. I_MUTEX_QUOTA);
  596. truncate_inode_pages(&sb_dqopt(sb)->files[cnt]->i_data, 0);
  597. mutex_unlock(&sb_dqopt(sb)->files[cnt]->i_mutex);
  598. }
  599. mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
  600. return 0;
  601. }
  602. EXPORT_SYMBOL(dquot_quota_sync);
  603. /* Free unused dquots from cache */
  604. static void prune_dqcache(int count)
  605. {
  606. struct list_head *head;
  607. struct dquot *dquot;
  608. head = free_dquots.prev;
  609. while (head != &free_dquots && count) {
  610. dquot = list_entry(head, struct dquot, dq_free);
  611. remove_dquot_hash(dquot);
  612. remove_free_dquot(dquot);
  613. remove_inuse(dquot);
  614. do_destroy_dquot(dquot);
  615. count--;
  616. head = free_dquots.prev;
  617. }
  618. }
  619. /*
  620. * This is called from kswapd when we think we need some
  621. * more memory
  622. */
  623. static int shrink_dqcache_memory(struct shrinker *shrink,
  624. struct shrink_control *sc)
  625. {
  626. int nr = sc->nr_to_scan;
  627. if (nr) {
  628. spin_lock(&dq_list_lock);
  629. prune_dqcache(nr);
  630. spin_unlock(&dq_list_lock);
  631. }
  632. return ((unsigned)
  633. percpu_counter_read_positive(&dqstats.counter[DQST_FREE_DQUOTS])
  634. /100) * sysctl_vfs_cache_pressure;
  635. }
  636. static struct shrinker dqcache_shrinker = {
  637. .shrink = shrink_dqcache_memory,
  638. .seeks = DEFAULT_SEEKS,
  639. };
  640. /*
  641. * Put reference to dquot
  642. * NOTE: If you change this function please check whether dqput_blocks() works right...
  643. */
  644. void dqput(struct dquot *dquot)
  645. {
  646. int ret;
  647. if (!dquot)
  648. return;
  649. #ifdef CONFIG_QUOTA_DEBUG
  650. if (!atomic_read(&dquot->dq_count)) {
  651. quota_error(dquot->dq_sb, "trying to free free dquot of %s %d",
  652. quotatypes[dquot->dq_type], dquot->dq_id);
  653. BUG();
  654. }
  655. #endif
  656. dqstats_inc(DQST_DROPS);
  657. we_slept:
  658. spin_lock(&dq_list_lock);
  659. if (atomic_read(&dquot->dq_count) > 1) {
  660. /* We have more than one user... nothing to do */
  661. atomic_dec(&dquot->dq_count);
  662. /* Releasing dquot during quotaoff phase? */
  663. if (!sb_has_quota_active(dquot->dq_sb, dquot->dq_type) &&
  664. atomic_read(&dquot->dq_count) == 1)
  665. wake_up(&dquot->dq_wait_unused);
  666. spin_unlock(&dq_list_lock);
  667. return;
  668. }
  669. /* Need to release dquot? */
  670. if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags) && dquot_dirty(dquot)) {
  671. spin_unlock(&dq_list_lock);
  672. /* Commit dquot before releasing */
  673. ret = dquot->dq_sb->dq_op->write_dquot(dquot);
  674. if (ret < 0) {
  675. quota_error(dquot->dq_sb, "Can't write quota structure"
  676. " (error %d). Quota may get out of sync!",
  677. ret);
  678. /*
  679. * We clear dirty bit anyway, so that we avoid
  680. * infinite loop here
  681. */
  682. spin_lock(&dq_list_lock);
  683. clear_dquot_dirty(dquot);
  684. spin_unlock(&dq_list_lock);
  685. }
  686. goto we_slept;
  687. }
  688. /* Clear flag in case dquot was inactive (something bad happened) */
  689. clear_dquot_dirty(dquot);
  690. if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
  691. spin_unlock(&dq_list_lock);
  692. dquot->dq_sb->dq_op->release_dquot(dquot);
  693. goto we_slept;
  694. }
  695. atomic_dec(&dquot->dq_count);
  696. #ifdef CONFIG_QUOTA_DEBUG
  697. /* sanity check */
  698. BUG_ON(!list_empty(&dquot->dq_free));
  699. #endif
  700. put_dquot_last(dquot);
  701. spin_unlock(&dq_list_lock);
  702. }
  703. EXPORT_SYMBOL(dqput);
  704. struct dquot *dquot_alloc(struct super_block *sb, int type)
  705. {
  706. return kmem_cache_zalloc(dquot_cachep, GFP_NOFS);
  707. }
  708. EXPORT_SYMBOL(dquot_alloc);
  709. static struct dquot *get_empty_dquot(struct super_block *sb, int type)
  710. {
  711. struct dquot *dquot;
  712. dquot = sb->dq_op->alloc_dquot(sb, type);
  713. if(!dquot)
  714. return NULL;
  715. mutex_init(&dquot->dq_lock);
  716. INIT_LIST_HEAD(&dquot->dq_free);
  717. INIT_LIST_HEAD(&dquot->dq_inuse);
  718. INIT_HLIST_NODE(&dquot->dq_hash);
  719. INIT_LIST_HEAD(&dquot->dq_dirty);
  720. init_waitqueue_head(&dquot->dq_wait_unused);
  721. dquot->dq_sb = sb;
  722. dquot->dq_type = type;
  723. atomic_set(&dquot->dq_count, 1);
  724. return dquot;
  725. }
  726. /*
  727. * Get reference to dquot
  728. *
  729. * Locking is slightly tricky here. We are guarded from parallel quotaoff()
  730. * destroying our dquot by:
  731. * a) checking for quota flags under dq_list_lock and
  732. * b) getting a reference to dquot before we release dq_list_lock
  733. */
  734. struct dquot *dqget(struct super_block *sb, unsigned int id, int type)
  735. {
  736. unsigned int hashent = hashfn(sb, id, type);
  737. struct dquot *dquot = NULL, *empty = NULL;
  738. if (!sb_has_quota_active(sb, type))
  739. return NULL;
  740. we_slept:
  741. spin_lock(&dq_list_lock);
  742. spin_lock(&dq_state_lock);
  743. if (!sb_has_quota_active(sb, type)) {
  744. spin_unlock(&dq_state_lock);
  745. spin_unlock(&dq_list_lock);
  746. goto out;
  747. }
  748. spin_unlock(&dq_state_lock);
  749. dquot = find_dquot(hashent, sb, id, type);
  750. if (!dquot) {
  751. if (!empty) {
  752. spin_unlock(&dq_list_lock);
  753. empty = get_empty_dquot(sb, type);
  754. if (!empty)
  755. schedule(); /* Try to wait for a moment... */
  756. goto we_slept;
  757. }
  758. dquot = empty;
  759. empty = NULL;
  760. dquot->dq_id = id;
  761. /* all dquots go on the inuse_list */
  762. put_inuse(dquot);
  763. /* hash it first so it can be found */
  764. insert_dquot_hash(dquot);
  765. spin_unlock(&dq_list_lock);
  766. dqstats_inc(DQST_LOOKUPS);
  767. } else {
  768. if (!atomic_read(&dquot->dq_count))
  769. remove_free_dquot(dquot);
  770. atomic_inc(&dquot->dq_count);
  771. spin_unlock(&dq_list_lock);
  772. dqstats_inc(DQST_CACHE_HITS);
  773. dqstats_inc(DQST_LOOKUPS);
  774. }
  775. /* Wait for dq_lock - after this we know that either dquot_release() is
  776. * already finished or it will be canceled due to dq_count > 1 test */
  777. wait_on_dquot(dquot);
  778. /* Read the dquot / allocate space in quota file */
  779. if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags) &&
  780. sb->dq_op->acquire_dquot(dquot) < 0) {
  781. dqput(dquot);
  782. dquot = NULL;
  783. goto out;
  784. }
  785. #ifdef CONFIG_QUOTA_DEBUG
  786. BUG_ON(!dquot->dq_sb); /* Has somebody invalidated entry under us? */
  787. #endif
  788. out:
  789. if (empty)
  790. do_destroy_dquot(empty);
  791. return dquot;
  792. }
  793. EXPORT_SYMBOL(dqget);
  794. static int dqinit_needed(struct inode *inode, int type)
  795. {
  796. int cnt;
  797. if (IS_NOQUOTA(inode))
  798. return 0;
  799. if (type != -1)
  800. return !inode->i_dquot[type];
  801. for (cnt = 0; cnt < MAXQUOTAS; cnt++)
  802. if (!inode->i_dquot[cnt])
  803. return 1;
  804. return 0;
  805. }
  806. /* This routine is guarded by dqonoff_mutex mutex */
  807. static void add_dquot_ref(struct super_block *sb, int type)
  808. {
  809. struct inode *inode, *old_inode = NULL;
  810. #ifdef CONFIG_QUOTA_DEBUG
  811. int reserved = 0;
  812. #endif
  813. spin_lock(&inode_sb_list_lock);
  814. list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
  815. spin_lock(&inode->i_lock);
  816. if ((inode->i_state & (I_FREEING|I_WILL_FREE|I_NEW)) ||
  817. !atomic_read(&inode->i_writecount) ||
  818. !dqinit_needed(inode, type)) {
  819. spin_unlock(&inode->i_lock);
  820. continue;
  821. }
  822. #ifdef CONFIG_QUOTA_DEBUG
  823. if (unlikely(inode_get_rsv_space(inode) > 0))
  824. reserved = 1;
  825. #endif
  826. __iget(inode);
  827. spin_unlock(&inode->i_lock);
  828. spin_unlock(&inode_sb_list_lock);
  829. iput(old_inode);
  830. __dquot_initialize(inode, type);
  831. /*
  832. * We hold a reference to 'inode' so it couldn't have been
  833. * removed from s_inodes list while we dropped the
  834. * inode_sb_list_lock We cannot iput the inode now as we can be
  835. * holding the last reference and we cannot iput it under
  836. * inode_sb_list_lock. So we keep the reference and iput it
  837. * later.
  838. */
  839. old_inode = inode;
  840. spin_lock(&inode_sb_list_lock);
  841. }
  842. spin_unlock(&inode_sb_list_lock);
  843. iput(old_inode);
  844. #ifdef CONFIG_QUOTA_DEBUG
  845. if (reserved) {
  846. quota_error(sb, "Writes happened before quota was turned on "
  847. "thus quota information is probably inconsistent. "
  848. "Please run quotacheck(8)");
  849. }
  850. #endif
  851. }
  852. /*
  853. * Return 0 if dqput() won't block.
  854. * (note that 1 doesn't necessarily mean blocking)
  855. */
  856. static inline int dqput_blocks(struct dquot *dquot)
  857. {
  858. if (atomic_read(&dquot->dq_count) <= 1)
  859. return 1;
  860. return 0;
  861. }
  862. /*
  863. * Remove references to dquots from inode and add dquot to list for freeing
  864. * if we have the last reference to dquot
  865. * We can't race with anybody because we hold dqptr_sem for writing...
  866. */
  867. static int remove_inode_dquot_ref(struct inode *inode, int type,
  868. struct list_head *tofree_head)
  869. {
  870. struct dquot *dquot = inode->i_dquot[type];
  871. inode->i_dquot[type] = NULL;
  872. if (dquot) {
  873. if (dqput_blocks(dquot)) {
  874. #ifdef CONFIG_QUOTA_DEBUG
  875. if (atomic_read(&dquot->dq_count) != 1)
  876. quota_error(inode->i_sb, "Adding dquot with "
  877. "dq_count %d to dispose list",
  878. atomic_read(&dquot->dq_count));
  879. #endif
  880. spin_lock(&dq_list_lock);
  881. /* As dquot must have currently users it can't be on
  882. * the free list... */
  883. list_add(&dquot->dq_free, tofree_head);
  884. spin_unlock(&dq_list_lock);
  885. return 1;
  886. }
  887. else
  888. dqput(dquot); /* We have guaranteed we won't block */
  889. }
  890. return 0;
  891. }
  892. /*
  893. * Free list of dquots
  894. * Dquots are removed from inodes and no new references can be got so we are
  895. * the only ones holding reference
  896. */
  897. static void put_dquot_list(struct list_head *tofree_head)
  898. {
  899. struct list_head *act_head;
  900. struct dquot *dquot;
  901. act_head = tofree_head->next;
  902. while (act_head != tofree_head) {
  903. dquot = list_entry(act_head, struct dquot, dq_free);
  904. act_head = act_head->next;
  905. /* Remove dquot from the list so we won't have problems... */
  906. list_del_init(&dquot->dq_free);
  907. dqput(dquot);
  908. }
  909. }
  910. static void remove_dquot_ref(struct super_block *sb, int type,
  911. struct list_head *tofree_head)
  912. {
  913. struct inode *inode;
  914. int reserved = 0;
  915. spin_lock(&inode_sb_list_lock);
  916. list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
  917. /*
  918. * We have to scan also I_NEW inodes because they can already
  919. * have quota pointer initialized. Luckily, we need to touch
  920. * only quota pointers and these have separate locking
  921. * (dqptr_sem).
  922. */
  923. if (!IS_NOQUOTA(inode)) {
  924. if (unlikely(inode_get_rsv_space(inode) > 0))
  925. reserved = 1;
  926. remove_inode_dquot_ref(inode, type, tofree_head);
  927. }
  928. }
  929. spin_unlock(&inode_sb_list_lock);
  930. #ifdef CONFIG_QUOTA_DEBUG
  931. if (reserved) {
  932. printk(KERN_WARNING "VFS (%s): Writes happened after quota"
  933. " was disabled thus quota information is probably "
  934. "inconsistent. Please run quotacheck(8).\n", sb->s_id);
  935. }
  936. #endif
  937. }
  938. /* Gather all references from inodes and drop them */
  939. static void drop_dquot_ref(struct super_block *sb, int type)
  940. {
  941. LIST_HEAD(tofree_head);
  942. if (sb->dq_op) {
  943. down_write(&sb_dqopt(sb)->dqptr_sem);
  944. remove_dquot_ref(sb, type, &tofree_head);
  945. up_write(&sb_dqopt(sb)->dqptr_sem);
  946. put_dquot_list(&tofree_head);
  947. }
  948. }
  949. static inline void dquot_incr_inodes(struct dquot *dquot, qsize_t number)
  950. {
  951. dquot->dq_dqb.dqb_curinodes += number;
  952. }
  953. static inline void dquot_incr_space(struct dquot *dquot, qsize_t number)
  954. {
  955. dquot->dq_dqb.dqb_curspace += number;
  956. }
  957. static inline void dquot_resv_space(struct dquot *dquot, qsize_t number)
  958. {
  959. dquot->dq_dqb.dqb_rsvspace += number;
  960. }
  961. /*
  962. * Claim reserved quota space
  963. */
  964. static void dquot_claim_reserved_space(struct dquot *dquot, qsize_t number)
  965. {
  966. if (dquot->dq_dqb.dqb_rsvspace < number) {
  967. WARN_ON_ONCE(1);
  968. number = dquot->dq_dqb.dqb_rsvspace;
  969. }
  970. dquot->dq_dqb.dqb_curspace += number;
  971. dquot->dq_dqb.dqb_rsvspace -= number;
  972. }
  973. static inline
  974. void dquot_free_reserved_space(struct dquot *dquot, qsize_t number)
  975. {
  976. if (dquot->dq_dqb.dqb_rsvspace >= number)
  977. dquot->dq_dqb.dqb_rsvspace -= number;
  978. else {
  979. WARN_ON_ONCE(1);
  980. dquot->dq_dqb.dqb_rsvspace = 0;
  981. }
  982. }
  983. static void dquot_decr_inodes(struct dquot *dquot, qsize_t number)
  984. {
  985. if (sb_dqopt(dquot->dq_sb)->flags & DQUOT_NEGATIVE_USAGE ||
  986. dquot->dq_dqb.dqb_curinodes >= number)
  987. dquot->dq_dqb.dqb_curinodes -= number;
  988. else
  989. dquot->dq_dqb.dqb_curinodes = 0;
  990. if (dquot->dq_dqb.dqb_curinodes <= dquot->dq_dqb.dqb_isoftlimit)
  991. dquot->dq_dqb.dqb_itime = (time_t) 0;
  992. clear_bit(DQ_INODES_B, &dquot->dq_flags);
  993. }
  994. static void dquot_decr_space(struct dquot *dquot, qsize_t number)
  995. {
  996. if (sb_dqopt(dquot->dq_sb)->flags & DQUOT_NEGATIVE_USAGE ||
  997. dquot->dq_dqb.dqb_curspace >= number)
  998. dquot->dq_dqb.dqb_curspace -= number;
  999. else
  1000. dquot->dq_dqb.dqb_curspace = 0;
  1001. if (dquot->dq_dqb.dqb_curspace <= dquot->dq_dqb.dqb_bsoftlimit)
  1002. dquot->dq_dqb.dqb_btime = (time_t) 0;
  1003. clear_bit(DQ_BLKS_B, &dquot->dq_flags);
  1004. }
  1005. static int warning_issued(struct dquot *dquot, const int warntype)
  1006. {
  1007. int flag = (warntype == QUOTA_NL_BHARDWARN ||
  1008. warntype == QUOTA_NL_BSOFTLONGWARN) ? DQ_BLKS_B :
  1009. ((warntype == QUOTA_NL_IHARDWARN ||
  1010. warntype == QUOTA_NL_ISOFTLONGWARN) ? DQ_INODES_B : 0);
  1011. if (!flag)
  1012. return 0;
  1013. return test_and_set_bit(flag, &dquot->dq_flags);
  1014. }
  1015. #ifdef CONFIG_PRINT_QUOTA_WARNING
  1016. static int flag_print_warnings = 1;
  1017. static int need_print_warning(struct dquot *dquot)
  1018. {
  1019. if (!flag_print_warnings)
  1020. return 0;
  1021. switch (dquot->dq_type) {
  1022. case USRQUOTA:
  1023. return current_fsuid() == dquot->dq_id;
  1024. case GRPQUOTA:
  1025. return in_group_p(dquot->dq_id);
  1026. }
  1027. return 0;
  1028. }
  1029. /* Print warning to user which exceeded quota */
  1030. static void print_warning(struct dquot *dquot, const int warntype)
  1031. {
  1032. char *msg = NULL;
  1033. struct tty_struct *tty;
  1034. if (warntype == QUOTA_NL_IHARDBELOW ||
  1035. warntype == QUOTA_NL_ISOFTBELOW ||
  1036. warntype == QUOTA_NL_BHARDBELOW ||
  1037. warntype == QUOTA_NL_BSOFTBELOW || !need_print_warning(dquot))
  1038. return;
  1039. tty = get_current_tty();
  1040. if (!tty)
  1041. return;
  1042. tty_write_message(tty, dquot->dq_sb->s_id);
  1043. if (warntype == QUOTA_NL_ISOFTWARN || warntype == QUOTA_NL_BSOFTWARN)
  1044. tty_write_message(tty, ": warning, ");
  1045. else
  1046. tty_write_message(tty, ": write failed, ");
  1047. tty_write_message(tty, quotatypes[dquot->dq_type]);
  1048. switch (warntype) {
  1049. case QUOTA_NL_IHARDWARN:
  1050. msg = " file limit reached.\r\n";
  1051. break;
  1052. case QUOTA_NL_ISOFTLONGWARN:
  1053. msg = " file quota exceeded too long.\r\n";
  1054. break;
  1055. case QUOTA_NL_ISOFTWARN:
  1056. msg = " file quota exceeded.\r\n";
  1057. break;
  1058. case QUOTA_NL_BHARDWARN:
  1059. msg = " block limit reached.\r\n";
  1060. break;
  1061. case QUOTA_NL_BSOFTLONGWARN:
  1062. msg = " block quota exceeded too long.\r\n";
  1063. break;
  1064. case QUOTA_NL_BSOFTWARN:
  1065. msg = " block quota exceeded.\r\n";
  1066. break;
  1067. }
  1068. tty_write_message(tty, msg);
  1069. tty_kref_put(tty);
  1070. }
  1071. #endif
  1072. /*
  1073. * Write warnings to the console and send warning messages over netlink.
  1074. *
  1075. * Note that this function can sleep.
  1076. */
  1077. static void flush_warnings(struct dquot *const *dquots, char *warntype)
  1078. {
  1079. struct dquot *dq;
  1080. int i;
  1081. for (i = 0; i < MAXQUOTAS; i++) {
  1082. dq = dquots[i];
  1083. if (dq && warntype[i] != QUOTA_NL_NOWARN &&
  1084. !warning_issued(dq, warntype[i])) {
  1085. #ifdef CONFIG_PRINT_QUOTA_WARNING
  1086. print_warning(dq, warntype[i]);
  1087. #endif
  1088. quota_send_warning(dq->dq_type, dq->dq_id,
  1089. dq->dq_sb->s_dev, warntype[i]);
  1090. }
  1091. }
  1092. }
  1093. static int ignore_hardlimit(struct dquot *dquot)
  1094. {
  1095. struct mem_dqinfo *info = &sb_dqopt(dquot->dq_sb)->info[dquot->dq_type];
  1096. return capable(CAP_SYS_RESOURCE) &&
  1097. (info->dqi_format->qf_fmt_id != QFMT_VFS_OLD ||
  1098. !(info->dqi_flags & V1_DQF_RSQUASH));
  1099. }
  1100. /* needs dq_data_lock */
  1101. static int check_idq(struct dquot *dquot, qsize_t inodes, char *warntype)
  1102. {
  1103. qsize_t newinodes = dquot->dq_dqb.dqb_curinodes + inodes;
  1104. *warntype = QUOTA_NL_NOWARN;
  1105. if (!sb_has_quota_limits_enabled(dquot->dq_sb, dquot->dq_type) ||
  1106. test_bit(DQ_FAKE_B, &dquot->dq_flags))
  1107. return 0;
  1108. if (dquot->dq_dqb.dqb_ihardlimit &&
  1109. newinodes > dquot->dq_dqb.dqb_ihardlimit &&
  1110. !ignore_hardlimit(dquot)) {
  1111. *warntype = QUOTA_NL_IHARDWARN;
  1112. return -EDQUOT;
  1113. }
  1114. if (dquot->dq_dqb.dqb_isoftlimit &&
  1115. newinodes > dquot->dq_dqb.dqb_isoftlimit &&
  1116. dquot->dq_dqb.dqb_itime &&
  1117. get_seconds() >= dquot->dq_dqb.dqb_itime &&
  1118. !ignore_hardlimit(dquot)) {
  1119. *warntype = QUOTA_NL_ISOFTLONGWARN;
  1120. return -EDQUOT;
  1121. }
  1122. if (dquot->dq_dqb.dqb_isoftlimit &&
  1123. newinodes > dquot->dq_dqb.dqb_isoftlimit &&
  1124. dquot->dq_dqb.dqb_itime == 0) {
  1125. *warntype = QUOTA_NL_ISOFTWARN;
  1126. dquot->dq_dqb.dqb_itime = get_seconds() +
  1127. sb_dqopt(dquot->dq_sb)->info[dquot->dq_type].dqi_igrace;
  1128. }
  1129. return 0;
  1130. }
  1131. /* needs dq_data_lock */
  1132. static int check_bdq(struct dquot *dquot, qsize_t space, int prealloc, char *warntype)
  1133. {
  1134. qsize_t tspace;
  1135. struct super_block *sb = dquot->dq_sb;
  1136. *warntype = QUOTA_NL_NOWARN;
  1137. if (!sb_has_quota_limits_enabled(sb, dquot->dq_type) ||
  1138. test_bit(DQ_FAKE_B, &dquot->dq_flags))
  1139. return 0;
  1140. tspace = dquot->dq_dqb.dqb_curspace + dquot->dq_dqb.dqb_rsvspace
  1141. + space;
  1142. if (dquot->dq_dqb.dqb_bhardlimit &&
  1143. tspace > dquot->dq_dqb.dqb_bhardlimit &&
  1144. !ignore_hardlimit(dquot)) {
  1145. if (!prealloc)
  1146. *warntype = QUOTA_NL_BHARDWARN;
  1147. return -EDQUOT;
  1148. }
  1149. if (dquot->dq_dqb.dqb_bsoftlimit &&
  1150. tspace > dquot->dq_dqb.dqb_bsoftlimit &&
  1151. dquot->dq_dqb.dqb_btime &&
  1152. get_seconds() >= dquot->dq_dqb.dqb_btime &&
  1153. !ignore_hardlimit(dquot)) {
  1154. if (!prealloc)
  1155. *warntype = QUOTA_NL_BSOFTLONGWARN;
  1156. return -EDQUOT;
  1157. }
  1158. if (dquot->dq_dqb.dqb_bsoftlimit &&
  1159. tspace > dquot->dq_dqb.dqb_bsoftlimit &&
  1160. dquot->dq_dqb.dqb_btime == 0) {
  1161. if (!prealloc) {
  1162. *warntype = QUOTA_NL_BSOFTWARN;
  1163. dquot->dq_dqb.dqb_btime = get_seconds() +
  1164. sb_dqopt(sb)->info[dquot->dq_type].dqi_bgrace;
  1165. }
  1166. else
  1167. /*
  1168. * We don't allow preallocation to exceed softlimit so exceeding will
  1169. * be always printed
  1170. */
  1171. return -EDQUOT;
  1172. }
  1173. return 0;
  1174. }
  1175. static int info_idq_free(struct dquot *dquot, qsize_t inodes)
  1176. {
  1177. qsize_t newinodes;
  1178. if (test_bit(DQ_FAKE_B, &dquot->dq_flags) ||
  1179. dquot->dq_dqb.dqb_curinodes <= dquot->dq_dqb.dqb_isoftlimit ||
  1180. !sb_has_quota_limits_enabled(dquot->dq_sb, dquot->dq_type))
  1181. return QUOTA_NL_NOWARN;
  1182. newinodes = dquot->dq_dqb.dqb_curinodes - inodes;
  1183. if (newinodes <= dquot->dq_dqb.dqb_isoftlimit)
  1184. return QUOTA_NL_ISOFTBELOW;
  1185. if (dquot->dq_dqb.dqb_curinodes >= dquot->dq_dqb.dqb_ihardlimit &&
  1186. newinodes < dquot->dq_dqb.dqb_ihardlimit)
  1187. return QUOTA_NL_IHARDBELOW;
  1188. return QUOTA_NL_NOWARN;
  1189. }
  1190. static int info_bdq_free(struct dquot *dquot, qsize_t space)
  1191. {
  1192. if (test_bit(DQ_FAKE_B, &dquot->dq_flags) ||
  1193. dquot->dq_dqb.dqb_curspace <= dquot->dq_dqb.dqb_bsoftlimit)
  1194. return QUOTA_NL_NOWARN;
  1195. if (dquot->dq_dqb.dqb_curspace - space <= dquot->dq_dqb.dqb_bsoftlimit)
  1196. return QUOTA_NL_BSOFTBELOW;
  1197. if (dquot->dq_dqb.dqb_curspace >= dquot->dq_dqb.dqb_bhardlimit &&
  1198. dquot->dq_dqb.dqb_curspace - space < dquot->dq_dqb.dqb_bhardlimit)
  1199. return QUOTA_NL_BHARDBELOW;
  1200. return QUOTA_NL_NOWARN;
  1201. }
  1202. static int dquot_active(const struct inode *inode)
  1203. {
  1204. struct super_block *sb = inode->i_sb;
  1205. if (IS_NOQUOTA(inode))
  1206. return 0;
  1207. return sb_any_quota_loaded(sb) & ~sb_any_quota_suspended(sb);
  1208. }
  1209. /*
  1210. * Initialize quota pointers in inode
  1211. *
  1212. * We do things in a bit complicated way but by that we avoid calling
  1213. * dqget() and thus filesystem callbacks under dqptr_sem.
  1214. *
  1215. * It is better to call this function outside of any transaction as it
  1216. * might need a lot of space in journal for dquot structure allocation.
  1217. */
  1218. static void __dquot_initialize(struct inode *inode, int type)
  1219. {
  1220. unsigned int id = 0;
  1221. int cnt;
  1222. struct dquot *got[MAXQUOTAS];
  1223. struct super_block *sb = inode->i_sb;
  1224. qsize_t rsv;
  1225. /* First test before acquiring mutex - solves deadlocks when we
  1226. * re-enter the quota code and are already holding the mutex */
  1227. if (!dquot_active(inode))
  1228. return;
  1229. /* First get references to structures we might need. */
  1230. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1231. got[cnt] = NULL;
  1232. if (type != -1 && cnt != type)
  1233. continue;
  1234. switch (cnt) {
  1235. case USRQUOTA:
  1236. id = inode->i_uid;
  1237. break;
  1238. case GRPQUOTA:
  1239. id = inode->i_gid;
  1240. break;
  1241. }
  1242. got[cnt] = dqget(sb, id, cnt);
  1243. }
  1244. down_write(&sb_dqopt(sb)->dqptr_sem);
  1245. if (IS_NOQUOTA(inode))
  1246. goto out_err;
  1247. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1248. if (type != -1 && cnt != type)
  1249. continue;
  1250. /* Avoid races with quotaoff() */
  1251. if (!sb_has_quota_active(sb, cnt))
  1252. continue;
  1253. /* We could race with quotaon or dqget() could have failed */
  1254. if (!got[cnt])
  1255. continue;
  1256. if (!inode->i_dquot[cnt]) {
  1257. inode->i_dquot[cnt] = got[cnt];
  1258. got[cnt] = NULL;
  1259. /*
  1260. * Make quota reservation system happy if someone
  1261. * did a write before quota was turned on
  1262. */
  1263. rsv = inode_get_rsv_space(inode);
  1264. if (unlikely(rsv))
  1265. dquot_resv_space(inode->i_dquot[cnt], rsv);
  1266. }
  1267. }
  1268. out_err:
  1269. up_write(&sb_dqopt(sb)->dqptr_sem);
  1270. /* Drop unused references */
  1271. dqput_all(got);
  1272. }
  1273. void dquot_initialize(struct inode *inode)
  1274. {
  1275. __dquot_initialize(inode, -1);
  1276. }
  1277. EXPORT_SYMBOL(dquot_initialize);
  1278. /*
  1279. * Release all quotas referenced by inode
  1280. */
  1281. static void __dquot_drop(struct inode *inode)
  1282. {
  1283. int cnt;
  1284. struct dquot *put[MAXQUOTAS];
  1285. down_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1286. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1287. put[cnt] = inode->i_dquot[cnt];
  1288. inode->i_dquot[cnt] = NULL;
  1289. }
  1290. up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1291. dqput_all(put);
  1292. }
  1293. void dquot_drop(struct inode *inode)
  1294. {
  1295. int cnt;
  1296. if (IS_NOQUOTA(inode))
  1297. return;
  1298. /*
  1299. * Test before calling to rule out calls from proc and such
  1300. * where we are not allowed to block. Note that this is
  1301. * actually reliable test even without the lock - the caller
  1302. * must assure that nobody can come after the DQUOT_DROP and
  1303. * add quota pointers back anyway.
  1304. */
  1305. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1306. if (inode->i_dquot[cnt])
  1307. break;
  1308. }
  1309. if (cnt < MAXQUOTAS)
  1310. __dquot_drop(inode);
  1311. }
  1312. EXPORT_SYMBOL(dquot_drop);
  1313. /*
  1314. * inode_reserved_space is managed internally by quota, and protected by
  1315. * i_lock similar to i_blocks+i_bytes.
  1316. */
  1317. static qsize_t *inode_reserved_space(struct inode * inode)
  1318. {
  1319. /* Filesystem must explicitly define it's own method in order to use
  1320. * quota reservation interface */
  1321. BUG_ON(!inode->i_sb->dq_op->get_reserved_space);
  1322. return inode->i_sb->dq_op->get_reserved_space(inode);
  1323. }
  1324. void inode_add_rsv_space(struct inode *inode, qsize_t number)
  1325. {
  1326. spin_lock(&inode->i_lock);
  1327. *inode_reserved_space(inode) += number;
  1328. spin_unlock(&inode->i_lock);
  1329. }
  1330. EXPORT_SYMBOL(inode_add_rsv_space);
  1331. void inode_claim_rsv_space(struct inode *inode, qsize_t number)
  1332. {
  1333. spin_lock(&inode->i_lock);
  1334. *inode_reserved_space(inode) -= number;
  1335. __inode_add_bytes(inode, number);
  1336. spin_unlock(&inode->i_lock);
  1337. }
  1338. EXPORT_SYMBOL(inode_claim_rsv_space);
  1339. void inode_sub_rsv_space(struct inode *inode, qsize_t number)
  1340. {
  1341. spin_lock(&inode->i_lock);
  1342. *inode_reserved_space(inode) -= number;
  1343. spin_unlock(&inode->i_lock);
  1344. }
  1345. EXPORT_SYMBOL(inode_sub_rsv_space);
  1346. static qsize_t inode_get_rsv_space(struct inode *inode)
  1347. {
  1348. qsize_t ret;
  1349. if (!inode->i_sb->dq_op->get_reserved_space)
  1350. return 0;
  1351. spin_lock(&inode->i_lock);
  1352. ret = *inode_reserved_space(inode);
  1353. spin_unlock(&inode->i_lock);
  1354. return ret;
  1355. }
  1356. static void inode_incr_space(struct inode *inode, qsize_t number,
  1357. int reserve)
  1358. {
  1359. if (reserve)
  1360. inode_add_rsv_space(inode, number);
  1361. else
  1362. inode_add_bytes(inode, number);
  1363. }
  1364. static void inode_decr_space(struct inode *inode, qsize_t number, int reserve)
  1365. {
  1366. if (reserve)
  1367. inode_sub_rsv_space(inode, number);
  1368. else
  1369. inode_sub_bytes(inode, number);
  1370. }
  1371. /*
  1372. * This functions updates i_blocks+i_bytes fields and quota information
  1373. * (together with appropriate checks).
  1374. *
  1375. * NOTE: We absolutely rely on the fact that caller dirties the inode
  1376. * (usually helpers in quotaops.h care about this) and holds a handle for
  1377. * the current transaction so that dquot write and inode write go into the
  1378. * same transaction.
  1379. */
  1380. /*
  1381. * This operation can block, but only after everything is updated
  1382. */
  1383. int __dquot_alloc_space(struct inode *inode, qsize_t number, int flags)
  1384. {
  1385. int cnt, ret = 0;
  1386. char warntype[MAXQUOTAS];
  1387. int warn = flags & DQUOT_SPACE_WARN;
  1388. int reserve = flags & DQUOT_SPACE_RESERVE;
  1389. int nofail = flags & DQUOT_SPACE_NOFAIL;
  1390. /*
  1391. * First test before acquiring mutex - solves deadlocks when we
  1392. * re-enter the quota code and are already holding the mutex
  1393. */
  1394. if (!dquot_active(inode)) {
  1395. inode_incr_space(inode, number, reserve);
  1396. goto out;
  1397. }
  1398. down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1399. for (cnt = 0; cnt < MAXQUOTAS; cnt++)
  1400. warntype[cnt] = QUOTA_NL_NOWARN;
  1401. spin_lock(&dq_data_lock);
  1402. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1403. if (!inode->i_dquot[cnt])
  1404. continue;
  1405. ret = check_bdq(inode->i_dquot[cnt], number, !warn,
  1406. warntype+cnt);
  1407. if (ret && !nofail) {
  1408. spin_unlock(&dq_data_lock);
  1409. goto out_flush_warn;
  1410. }
  1411. }
  1412. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1413. if (!inode->i_dquot[cnt])
  1414. continue;
  1415. if (reserve)
  1416. dquot_resv_space(inode->i_dquot[cnt], number);
  1417. else
  1418. dquot_incr_space(inode->i_dquot[cnt], number);
  1419. }
  1420. inode_incr_space(inode, number, reserve);
  1421. spin_unlock(&dq_data_lock);
  1422. if (reserve)
  1423. goto out_flush_warn;
  1424. mark_all_dquot_dirty(inode->i_dquot);
  1425. out_flush_warn:
  1426. flush_warnings(inode->i_dquot, warntype);
  1427. up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1428. out:
  1429. return ret;
  1430. }
  1431. EXPORT_SYMBOL(__dquot_alloc_space);
  1432. /*
  1433. * This operation can block, but only after everything is updated
  1434. */
  1435. int dquot_alloc_inode(const struct inode *inode)
  1436. {
  1437. int cnt, ret = 0;
  1438. char warntype[MAXQUOTAS];
  1439. /* First test before acquiring mutex - solves deadlocks when we
  1440. * re-enter the quota code and are already holding the mutex */
  1441. if (!dquot_active(inode))
  1442. return 0;
  1443. for (cnt = 0; cnt < MAXQUOTAS; cnt++)
  1444. warntype[cnt] = QUOTA_NL_NOWARN;
  1445. down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1446. spin_lock(&dq_data_lock);
  1447. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1448. if (!inode->i_dquot[cnt])
  1449. continue;
  1450. ret = check_idq(inode->i_dquot[cnt], 1, warntype + cnt);
  1451. if (ret)
  1452. goto warn_put_all;
  1453. }
  1454. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1455. if (!inode->i_dquot[cnt])
  1456. continue;
  1457. dquot_incr_inodes(inode->i_dquot[cnt], 1);
  1458. }
  1459. warn_put_all:
  1460. spin_unlock(&dq_data_lock);
  1461. if (ret == 0)
  1462. mark_all_dquot_dirty(inode->i_dquot);
  1463. flush_warnings(inode->i_dquot, warntype);
  1464. up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1465. return ret;
  1466. }
  1467. EXPORT_SYMBOL(dquot_alloc_inode);
  1468. /*
  1469. * Convert in-memory reserved quotas to real consumed quotas
  1470. */
  1471. int dquot_claim_space_nodirty(struct inode *inode, qsize_t number)
  1472. {
  1473. int cnt;
  1474. if (!dquot_active(inode)) {
  1475. inode_claim_rsv_space(inode, number);
  1476. return 0;
  1477. }
  1478. down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1479. spin_lock(&dq_data_lock);
  1480. /* Claim reserved quotas to allocated quotas */
  1481. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1482. if (inode->i_dquot[cnt])
  1483. dquot_claim_reserved_space(inode->i_dquot[cnt],
  1484. number);
  1485. }
  1486. /* Update inode bytes */
  1487. inode_claim_rsv_space(inode, number);
  1488. spin_unlock(&dq_data_lock);
  1489. mark_all_dquot_dirty(inode->i_dquot);
  1490. up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1491. return 0;
  1492. }
  1493. EXPORT_SYMBOL(dquot_claim_space_nodirty);
  1494. /*
  1495. * This operation can block, but only after everything is updated
  1496. */
  1497. void __dquot_free_space(struct inode *inode, qsize_t number, int flags)
  1498. {
  1499. unsigned int cnt;
  1500. char warntype[MAXQUOTAS];
  1501. int reserve = flags & DQUOT_SPACE_RESERVE;
  1502. /* First test before acquiring mutex - solves deadlocks when we
  1503. * re-enter the quota code and are already holding the mutex */
  1504. if (!dquot_active(inode)) {
  1505. inode_decr_space(inode, number, reserve);
  1506. return;
  1507. }
  1508. down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1509. spin_lock(&dq_data_lock);
  1510. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1511. if (!inode->i_dquot[cnt])
  1512. continue;
  1513. warntype[cnt] = info_bdq_free(inode->i_dquot[cnt], number);
  1514. if (reserve)
  1515. dquot_free_reserved_space(inode->i_dquot[cnt], number);
  1516. else
  1517. dquot_decr_space(inode->i_dquot[cnt], number);
  1518. }
  1519. inode_decr_space(inode, number, reserve);
  1520. spin_unlock(&dq_data_lock);
  1521. if (reserve)
  1522. goto out_unlock;
  1523. mark_all_dquot_dirty(inode->i_dquot);
  1524. out_unlock:
  1525. flush_warnings(inode->i_dquot, warntype);
  1526. up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1527. }
  1528. EXPORT_SYMBOL(__dquot_free_space);
  1529. /*
  1530. * This operation can block, but only after everything is updated
  1531. */
  1532. void dquot_free_inode(const struct inode *inode)
  1533. {
  1534. unsigned int cnt;
  1535. char warntype[MAXQUOTAS];
  1536. /* First test before acquiring mutex - solves deadlocks when we
  1537. * re-enter the quota code and are already holding the mutex */
  1538. if (!dquot_active(inode))
  1539. return;
  1540. down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1541. spin_lock(&dq_data_lock);
  1542. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1543. if (!inode->i_dquot[cnt])
  1544. continue;
  1545. warntype[cnt] = info_idq_free(inode->i_dquot[cnt], 1);
  1546. dquot_decr_inodes(inode->i_dquot[cnt], 1);
  1547. }
  1548. spin_unlock(&dq_data_lock);
  1549. mark_all_dquot_dirty(inode->i_dquot);
  1550. flush_warnings(inode->i_dquot, warntype);
  1551. up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1552. }
  1553. EXPORT_SYMBOL(dquot_free_inode);
  1554. /*
  1555. * Transfer the number of inode and blocks from one diskquota to an other.
  1556. * On success, dquot references in transfer_to are consumed and references
  1557. * to original dquots that need to be released are placed there. On failure,
  1558. * references are kept untouched.
  1559. *
  1560. * This operation can block, but only after everything is updated
  1561. * A transaction must be started when entering this function.
  1562. *
  1563. */
  1564. int __dquot_transfer(struct inode *inode, struct dquot **transfer_to)
  1565. {
  1566. qsize_t space, cur_space;
  1567. qsize_t rsv_space = 0;
  1568. struct dquot *transfer_from[MAXQUOTAS] = {};
  1569. int cnt, ret = 0;
  1570. char is_valid[MAXQUOTAS] = {};
  1571. char warntype_to[MAXQUOTAS];
  1572. char warntype_from_inodes[MAXQUOTAS], warntype_from_space[MAXQUOTAS];
  1573. /* First test before acquiring mutex - solves deadlocks when we
  1574. * re-enter the quota code and are already holding the mutex */
  1575. if (IS_NOQUOTA(inode))
  1576. return 0;
  1577. /* Initialize the arrays */
  1578. for (cnt = 0; cnt < MAXQUOTAS; cnt++)
  1579. warntype_to[cnt] = QUOTA_NL_NOWARN;
  1580. down_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1581. if (IS_NOQUOTA(inode)) { /* File without quota accounting? */
  1582. up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1583. return 0;
  1584. }
  1585. spin_lock(&dq_data_lock);
  1586. cur_space = inode_get_bytes(inode);
  1587. rsv_space = inode_get_rsv_space(inode);
  1588. space = cur_space + rsv_space;
  1589. /* Build the transfer_from list and check the limits */
  1590. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1591. /*
  1592. * Skip changes for same uid or gid or for turned off quota-type.
  1593. */
  1594. if (!transfer_to[cnt])
  1595. continue;
  1596. /* Avoid races with quotaoff() */
  1597. if (!sb_has_quota_active(inode->i_sb, cnt))
  1598. continue;
  1599. is_valid[cnt] = 1;
  1600. transfer_from[cnt] = inode->i_dquot[cnt];
  1601. ret = check_idq(transfer_to[cnt], 1, warntype_to + cnt);
  1602. if (ret)
  1603. goto over_quota;
  1604. ret = check_bdq(transfer_to[cnt], space, 0, warntype_to + cnt);
  1605. if (ret)
  1606. goto over_quota;
  1607. }
  1608. /*
  1609. * Finally perform the needed transfer from transfer_from to transfer_to
  1610. */
  1611. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1612. if (!is_valid[cnt])
  1613. continue;
  1614. /* Due to IO error we might not have transfer_from[] structure */
  1615. if (transfer_from[cnt]) {
  1616. warntype_from_inodes[cnt] =
  1617. info_idq_free(transfer_from[cnt], 1);
  1618. warntype_from_space[cnt] =
  1619. info_bdq_free(transfer_from[cnt], space);
  1620. dquot_decr_inodes(transfer_from[cnt], 1);
  1621. dquot_decr_space(transfer_from[cnt], cur_space);
  1622. dquot_free_reserved_space(transfer_from[cnt],
  1623. rsv_space);
  1624. }
  1625. dquot_incr_inodes(transfer_to[cnt], 1);
  1626. dquot_incr_space(transfer_to[cnt], cur_space);
  1627. dquot_resv_space(transfer_to[cnt], rsv_space);
  1628. inode->i_dquot[cnt] = transfer_to[cnt];
  1629. }
  1630. spin_unlock(&dq_data_lock);
  1631. up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1632. mark_all_dquot_dirty(transfer_from);
  1633. mark_all_dquot_dirty(transfer_to);
  1634. flush_warnings(transfer_to, warntype_to);
  1635. flush_warnings(transfer_from, warntype_from_inodes);
  1636. flush_warnings(transfer_from, warntype_from_space);
  1637. /* Pass back references to put */
  1638. for (cnt = 0; cnt < MAXQUOTAS; cnt++)
  1639. if (is_valid[cnt])
  1640. transfer_to[cnt] = transfer_from[cnt];
  1641. return 0;
  1642. over_quota:
  1643. spin_unlock(&dq_data_lock);
  1644. up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1645. flush_warnings(transfer_to, warntype_to);
  1646. return ret;
  1647. }
  1648. EXPORT_SYMBOL(__dquot_transfer);
  1649. /* Wrapper for transferring ownership of an inode for uid/gid only
  1650. * Called from FSXXX_setattr()
  1651. */
  1652. int dquot_transfer(struct inode *inode, struct iattr *iattr)
  1653. {
  1654. struct dquot *transfer_to[MAXQUOTAS] = {};
  1655. struct super_block *sb = inode->i_sb;
  1656. int ret;
  1657. if (!dquot_active(inode))
  1658. return 0;
  1659. if (iattr->ia_valid & ATTR_UID && iattr->ia_uid != inode->i_uid)
  1660. transfer_to[USRQUOTA] = dqget(sb, iattr->ia_uid, USRQUOTA);
  1661. if (iattr->ia_valid & ATTR_GID && iattr->ia_gid != inode->i_gid)
  1662. transfer_to[GRPQUOTA] = dqget(sb, iattr->ia_gid, GRPQUOTA);
  1663. ret = __dquot_transfer(inode, transfer_to);
  1664. dqput_all(transfer_to);
  1665. return ret;
  1666. }
  1667. EXPORT_SYMBOL(dquot_transfer);
  1668. /*
  1669. * Write info of quota file to disk
  1670. */
  1671. int dquot_commit_info(struct super_block *sb, int type)
  1672. {
  1673. int ret;
  1674. struct quota_info *dqopt = sb_dqopt(sb);
  1675. mutex_lock(&dqopt->dqio_mutex);
  1676. ret = dqopt->ops[type]->write_file_info(sb, type);
  1677. mutex_unlock(&dqopt->dqio_mutex);
  1678. return ret;
  1679. }
  1680. EXPORT_SYMBOL(dquot_commit_info);
  1681. /*
  1682. * Definitions of diskquota operations.
  1683. */
  1684. const struct dquot_operations dquot_operations = {
  1685. .write_dquot = dquot_commit,
  1686. .acquire_dquot = dquot_acquire,
  1687. .release_dquot = dquot_release,
  1688. .mark_dirty = dquot_mark_dquot_dirty,
  1689. .write_info = dquot_commit_info,
  1690. .alloc_dquot = dquot_alloc,
  1691. .destroy_dquot = dquot_destroy,
  1692. };
  1693. EXPORT_SYMBOL(dquot_operations);
  1694. /*
  1695. * Generic helper for ->open on filesystems supporting disk quotas.
  1696. */
  1697. int dquot_file_open(struct inode *inode, struct file *file)
  1698. {
  1699. int error;
  1700. error = generic_file_open(inode, file);
  1701. if (!error && (file->f_mode & FMODE_WRITE))
  1702. dquot_initialize(inode);
  1703. return error;
  1704. }
  1705. EXPORT_SYMBOL(dquot_file_open);
  1706. /*
  1707. * Turn quota off on a device. type == -1 ==> quotaoff for all types (umount)
  1708. */
  1709. int dquot_disable(struct super_block *sb, int type, unsigned int flags)
  1710. {
  1711. int cnt, ret = 0;
  1712. struct quota_info *dqopt = sb_dqopt(sb);
  1713. struct inode *toputinode[MAXQUOTAS];
  1714. /* Cannot turn off usage accounting without turning off limits, or
  1715. * suspend quotas and simultaneously turn quotas off. */
  1716. if ((flags & DQUOT_USAGE_ENABLED && !(flags & DQUOT_LIMITS_ENABLED))
  1717. || (flags & DQUOT_SUSPENDED && flags & (DQUOT_LIMITS_ENABLED |
  1718. DQUOT_USAGE_ENABLED)))
  1719. return -EINVAL;
  1720. /* We need to serialize quota_off() for device */
  1721. mutex_lock(&dqopt->dqonoff_mutex);
  1722. /*
  1723. * Skip everything if there's nothing to do. We have to do this because
  1724. * sometimes we are called when fill_super() failed and calling
  1725. * sync_fs() in such cases does no good.
  1726. */
  1727. if (!sb_any_quota_loaded(sb)) {
  1728. mutex_unlock(&dqopt->dqonoff_mutex);
  1729. return 0;
  1730. }
  1731. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1732. toputinode[cnt] = NULL;
  1733. if (type != -1 && cnt != type)
  1734. continue;
  1735. if (!sb_has_quota_loaded(sb, cnt))
  1736. continue;
  1737. if (flags & DQUOT_SUSPENDED) {
  1738. spin_lock(&dq_state_lock);
  1739. dqopt->flags |=
  1740. dquot_state_flag(DQUOT_SUSPENDED, cnt);
  1741. spin_unlock(&dq_state_lock);
  1742. } else {
  1743. spin_lock(&dq_state_lock);
  1744. dqopt->flags &= ~dquot_state_flag(flags, cnt);
  1745. /* Turning off suspended quotas? */
  1746. if (!sb_has_quota_loaded(sb, cnt) &&
  1747. sb_has_quota_suspended(sb, cnt)) {
  1748. dqopt->flags &= ~dquot_state_flag(
  1749. DQUOT_SUSPENDED, cnt);
  1750. spin_unlock(&dq_state_lock);
  1751. iput(dqopt->files[cnt]);
  1752. dqopt->files[cnt] = NULL;
  1753. continue;
  1754. }
  1755. spin_unlock(&dq_state_lock);
  1756. }
  1757. /* We still have to keep quota loaded? */
  1758. if (sb_has_quota_loaded(sb, cnt) && !(flags & DQUOT_SUSPENDED))
  1759. continue;
  1760. /* Note: these are blocking operations */
  1761. drop_dquot_ref(sb, cnt);
  1762. invalidate_dquots(sb, cnt);
  1763. /*
  1764. * Now all dquots should be invalidated, all writes done so we
  1765. * should be only users of the info. No locks needed.
  1766. */
  1767. if (info_dirty(&dqopt->info[cnt]))
  1768. sb->dq_op->write_info(sb, cnt);
  1769. if (dqopt->ops[cnt]->free_file_info)
  1770. dqopt->ops[cnt]->free_file_info(sb, cnt);
  1771. put_quota_format(dqopt->info[cnt].dqi_format);
  1772. toputinode[cnt] = dqopt->files[cnt];
  1773. if (!sb_has_quota_loaded(sb, cnt))
  1774. dqopt->files[cnt] = NULL;
  1775. dqopt->info[cnt].dqi_flags = 0;
  1776. dqopt->info[cnt].dqi_igrace = 0;
  1777. dqopt->info[cnt].dqi_bgrace = 0;
  1778. dqopt->ops[cnt] = NULL;
  1779. }
  1780. mutex_unlock(&dqopt->dqonoff_mutex);
  1781. /* Skip syncing and setting flags if quota files are hidden */
  1782. if (dqopt->flags & DQUOT_QUOTA_SYS_FILE)
  1783. goto put_inodes;
  1784. /* Sync the superblock so that buffers with quota data are written to
  1785. * disk (and so userspace sees correct data afterwards). */
  1786. if (sb->s_op->sync_fs)
  1787. sb->s_op->sync_fs(sb, 1);
  1788. sync_blockdev(sb->s_bdev);
  1789. /* Now the quota files are just ordinary files and we can set the
  1790. * inode flags back. Moreover we discard the pagecache so that
  1791. * userspace sees the writes we did bypassing the pagecache. We
  1792. * must also discard the blockdev buffers so that we see the
  1793. * changes done by userspace on the next quotaon() */
  1794. for (cnt = 0; cnt < MAXQUOTAS; cnt++)
  1795. if (toputinode[cnt]) {
  1796. mutex_lock(&dqopt->dqonoff_mutex);
  1797. /* If quota was reenabled in the meantime, we have
  1798. * nothing to do */
  1799. if (!sb_has_quota_loaded(sb, cnt)) {
  1800. mutex_lock_nested(&toputinode[cnt]->i_mutex,
  1801. I_MUTEX_QUOTA);
  1802. toputinode[cnt]->i_flags &= ~(S_IMMUTABLE |
  1803. S_NOATIME | S_NOQUOTA);
  1804. truncate_inode_pages(&toputinode[cnt]->i_data,
  1805. 0);
  1806. mutex_unlock(&toputinode[cnt]->i_mutex);
  1807. mark_inode_dirty_sync(toputinode[cnt]);
  1808. }
  1809. mutex_unlock(&dqopt->dqonoff_mutex);
  1810. }
  1811. if (sb->s_bdev)
  1812. invalidate_bdev(sb->s_bdev);
  1813. put_inodes:
  1814. for (cnt = 0; cnt < MAXQUOTAS; cnt++)
  1815. if (toputinode[cnt]) {
  1816. /* On remount RO, we keep the inode pointer so that we
  1817. * can reenable quota on the subsequent remount RW. We
  1818. * have to check 'flags' variable and not use sb_has_
  1819. * function because another quotaon / quotaoff could
  1820. * change global state before we got here. We refuse
  1821. * to suspend quotas when there is pending delete on
  1822. * the quota file... */
  1823. if (!(flags & DQUOT_SUSPENDED))
  1824. iput(toputinode[cnt]);
  1825. else if (!toputinode[cnt]->i_nlink)
  1826. ret = -EBUSY;
  1827. }
  1828. return ret;
  1829. }
  1830. EXPORT_SYMBOL(dquot_disable);
  1831. int dquot_quota_off(struct super_block *sb, int type)
  1832. {
  1833. return dquot_disable(sb, type,
  1834. DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED);
  1835. }
  1836. EXPORT_SYMBOL(dquot_quota_off);
  1837. /*
  1838. * Turn quotas on on a device
  1839. */
  1840. /*
  1841. * Helper function to turn quotas on when we already have the inode of
  1842. * quota file and no quota information is loaded.
  1843. */
  1844. static int vfs_load_quota_inode(struct inode *inode, int type, int format_id,
  1845. unsigned int flags)
  1846. {
  1847. struct quota_format_type *fmt = find_quota_format(format_id);
  1848. struct super_block *sb = inode->i_sb;
  1849. struct quota_info *dqopt = sb_dqopt(sb);
  1850. int error;
  1851. int oldflags = -1;
  1852. if (!fmt)
  1853. return -ESRCH;
  1854. if (!S_ISREG(inode->i_mode)) {
  1855. error = -EACCES;
  1856. goto out_fmt;
  1857. }
  1858. if (IS_RDONLY(inode)) {
  1859. error = -EROFS;
  1860. goto out_fmt;
  1861. }
  1862. if (!sb->s_op->quota_write || !sb->s_op->quota_read) {
  1863. error = -EINVAL;
  1864. goto out_fmt;
  1865. }
  1866. /* Usage always has to be set... */
  1867. if (!(flags & DQUOT_USAGE_ENABLED)) {
  1868. error = -EINVAL;
  1869. goto out_fmt;
  1870. }
  1871. if (!(dqopt->flags & DQUOT_QUOTA_SYS_FILE)) {
  1872. /* As we bypass the pagecache we must now flush all the
  1873. * dirty data and invalidate caches so that kernel sees
  1874. * changes from userspace. It is not enough to just flush
  1875. * the quota file since if blocksize < pagesize, invalidation
  1876. * of the cache could fail because of other unrelated dirty
  1877. * data */
  1878. sync_filesystem(sb);
  1879. invalidate_bdev(sb->s_bdev);
  1880. }
  1881. mutex_lock(&dqopt->dqonoff_mutex);
  1882. if (sb_has_quota_loaded(sb, type)) {
  1883. error = -EBUSY;
  1884. goto out_lock;
  1885. }
  1886. if (!(dqopt->flags & DQUOT_QUOTA_SYS_FILE)) {
  1887. /* We don't want quota and atime on quota files (deadlocks
  1888. * possible) Also nobody should write to the file - we use
  1889. * special IO operations which ignore the immutable bit. */
  1890. mutex_lock_nested(&inode->i_mutex, I_MUTEX_QUOTA);
  1891. oldflags = inode->i_flags & (S_NOATIME | S_IMMUTABLE |
  1892. S_NOQUOTA);
  1893. inode->i_flags |= S_NOQUOTA | S_NOATIME | S_IMMUTABLE;
  1894. mutex_unlock(&inode->i_mutex);
  1895. /*
  1896. * When S_NOQUOTA is set, remove dquot references as no more
  1897. * references can be added
  1898. */
  1899. __dquot_drop(inode);
  1900. }
  1901. error = -EIO;
  1902. dqopt->files[type] = igrab(inode);
  1903. if (!dqopt->files[type])
  1904. goto out_lock;
  1905. error = -EINVAL;
  1906. if (!fmt->qf_ops->check_quota_file(sb, type))
  1907. goto out_file_init;
  1908. dqopt->ops[type] = fmt->qf_ops;
  1909. dqopt->info[type].dqi_format = fmt;
  1910. dqopt->info[type].dqi_fmt_id = format_id;
  1911. INIT_LIST_HEAD(&dqopt->info[type].dqi_dirty_list);
  1912. mutex_lock(&dqopt->dqio_mutex);
  1913. error = dqopt->ops[type]->read_file_info(sb, type);
  1914. if (error < 0) {
  1915. mutex_unlock(&dqopt->dqio_mutex);
  1916. goto out_file_init;
  1917. }
  1918. mutex_unlock(&dqopt->dqio_mutex);
  1919. spin_lock(&dq_state_lock);
  1920. dqopt->flags |= dquot_state_flag(flags, type);
  1921. spin_unlock(&dq_state_lock);
  1922. add_dquot_ref(sb, type);
  1923. mutex_unlock(&dqopt->dqonoff_mutex);
  1924. return 0;
  1925. out_file_init:
  1926. dqopt->files[type] = NULL;
  1927. iput(inode);
  1928. out_lock:
  1929. if (oldflags != -1) {
  1930. mutex_lock_nested(&inode->i_mutex, I_MUTEX_QUOTA);
  1931. /* Set the flags back (in the case of accidental quotaon()
  1932. * on a wrong file we don't want to mess up the flags) */
  1933. inode->i_flags &= ~(S_NOATIME | S_NOQUOTA | S_IMMUTABLE);
  1934. inode->i_flags |= oldflags;
  1935. mutex_unlock(&inode->i_mutex);
  1936. }
  1937. mutex_unlock(&dqopt->dqonoff_mutex);
  1938. out_fmt:
  1939. put_quota_format(fmt);
  1940. return error;
  1941. }
  1942. /* Reenable quotas on remount RW */
  1943. int dquot_resume(struct super_block *sb, int type)
  1944. {
  1945. struct quota_info *dqopt = sb_dqopt(sb);
  1946. struct inode *inode;
  1947. int ret = 0, cnt;
  1948. unsigned int flags;
  1949. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1950. if (type != -1 && cnt != type)
  1951. continue;
  1952. mutex_lock(&dqopt->dqonoff_mutex);
  1953. if (!sb_has_quota_suspended(sb, cnt)) {
  1954. mutex_unlock(&dqopt->dqonoff_mutex);
  1955. continue;
  1956. }
  1957. inode = dqopt->files[cnt];
  1958. dqopt->files[cnt] = NULL;
  1959. spin_lock(&dq_state_lock);
  1960. flags = dqopt->flags & dquot_state_flag(DQUOT_USAGE_ENABLED |
  1961. DQUOT_LIMITS_ENABLED,
  1962. cnt);
  1963. dqopt->flags &= ~dquot_state_flag(DQUOT_STATE_FLAGS, cnt);
  1964. spin_unlock(&dq_state_lock);
  1965. mutex_unlock(&dqopt->dqonoff_mutex);
  1966. flags = dquot_generic_flag(flags, cnt);
  1967. ret = vfs_load_quota_inode(inode, cnt,
  1968. dqopt->info[cnt].dqi_fmt_id, flags);
  1969. iput(inode);
  1970. }
  1971. return ret;
  1972. }
  1973. EXPORT_SYMBOL(dquot_resume);
  1974. int dquot_quota_on(struct super_block *sb, int type, int format_id,
  1975. struct path *path)
  1976. {
  1977. int error = security_quota_on(path->dentry);
  1978. if (error)
  1979. return error;
  1980. /* Quota file not on the same filesystem? */
  1981. if (path->dentry->d_sb != sb)
  1982. error = -EXDEV;
  1983. else
  1984. error = vfs_load_quota_inode(path->dentry->d_inode, type,
  1985. format_id, DQUOT_USAGE_ENABLED |
  1986. DQUOT_LIMITS_ENABLED);
  1987. return error;
  1988. }
  1989. EXPORT_SYMBOL(dquot_quota_on);
  1990. /*
  1991. * More powerful function for turning on quotas allowing setting
  1992. * of individual quota flags
  1993. */
  1994. int dquot_enable(struct inode *inode, int type, int format_id,
  1995. unsigned int flags)
  1996. {
  1997. int ret = 0;
  1998. struct super_block *sb = inode->i_sb;
  1999. struct quota_info *dqopt = sb_dqopt(sb);
  2000. /* Just unsuspend quotas? */
  2001. BUG_ON(flags & DQUOT_SUSPENDED);
  2002. if (!flags)
  2003. return 0;
  2004. /* Just updating flags needed? */
  2005. if (sb_has_quota_loaded(sb, type)) {
  2006. mutex_lock(&dqopt->dqonoff_mutex);
  2007. /* Now do a reliable test... */
  2008. if (!sb_has_quota_loaded(sb, type)) {
  2009. mutex_unlock(&dqopt->dqonoff_mutex);
  2010. goto load_quota;
  2011. }
  2012. if (flags & DQUOT_USAGE_ENABLED &&
  2013. sb_has_quota_usage_enabled(sb, type)) {
  2014. ret = -EBUSY;
  2015. goto out_lock;
  2016. }
  2017. if (flags & DQUOT_LIMITS_ENABLED &&
  2018. sb_has_quota_limits_enabled(sb, type)) {
  2019. ret = -EBUSY;
  2020. goto out_lock;
  2021. }
  2022. spin_lock(&dq_state_lock);
  2023. sb_dqopt(sb)->flags |= dquot_state_flag(flags, type);
  2024. spin_unlock(&dq_state_lock);
  2025. out_lock:
  2026. mutex_unlock(&dqopt->dqonoff_mutex);
  2027. return ret;
  2028. }
  2029. load_quota:
  2030. return vfs_load_quota_inode(inode, type, format_id, flags);
  2031. }
  2032. EXPORT_SYMBOL(dquot_enable);
  2033. /*
  2034. * This function is used when filesystem needs to initialize quotas
  2035. * during mount time.
  2036. */
  2037. int dquot_quota_on_mount(struct super_block *sb, char *qf_name,
  2038. int format_id, int type)
  2039. {
  2040. struct dentry *dentry;
  2041. int error;
  2042. mutex_lock(&sb->s_root->d_inode->i_mutex);
  2043. dentry = lookup_one_len(qf_name, sb->s_root, strlen(qf_name));
  2044. mutex_unlock(&sb->s_root->d_inode->i_mutex);
  2045. if (IS_ERR(dentry))
  2046. return PTR_ERR(dentry);
  2047. if (!dentry->d_inode) {
  2048. error = -ENOENT;
  2049. goto out;
  2050. }
  2051. error = security_quota_on(dentry);
  2052. if (!error)
  2053. error = vfs_load_quota_inode(dentry->d_inode, type, format_id,
  2054. DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED);
  2055. out:
  2056. dput(dentry);
  2057. return error;
  2058. }
  2059. EXPORT_SYMBOL(dquot_quota_on_mount);
  2060. static inline qsize_t qbtos(qsize_t blocks)
  2061. {
  2062. return blocks << QIF_DQBLKSIZE_BITS;
  2063. }
  2064. static inline qsize_t stoqb(qsize_t space)
  2065. {
  2066. return (space + QIF_DQBLKSIZE - 1) >> QIF_DQBLKSIZE_BITS;
  2067. }
  2068. /* Generic routine for getting common part of quota structure */
  2069. static void do_get_dqblk(struct dquot *dquot, struct fs_disk_quota *di)
  2070. {
  2071. struct mem_dqblk *dm = &dquot->dq_dqb;
  2072. memset(di, 0, sizeof(*di));
  2073. di->d_version = FS_DQUOT_VERSION;
  2074. di->d_flags = dquot->dq_type == USRQUOTA ?
  2075. FS_USER_QUOTA : FS_GROUP_QUOTA;
  2076. di->d_id = dquot->dq_id;
  2077. spin_lock(&dq_data_lock);
  2078. di->d_blk_hardlimit = stoqb(dm->dqb_bhardlimit);
  2079. di->d_blk_softlimit = stoqb(dm->dqb_bsoftlimit);
  2080. di->d_ino_hardlimit = dm->dqb_ihardlimit;
  2081. di->d_ino_softlimit = dm->dqb_isoftlimit;
  2082. di->d_bcount = dm->dqb_curspace + dm->dqb_rsvspace;
  2083. di->d_icount = dm->dqb_curinodes;
  2084. di->d_btimer = dm->dqb_btime;
  2085. di->d_itimer = dm->dqb_itime;
  2086. spin_unlock(&dq_data_lock);
  2087. }
  2088. int dquot_get_dqblk(struct super_block *sb, int type, qid_t id,
  2089. struct fs_disk_quota *di)
  2090. {
  2091. struct dquot *dquot;
  2092. dquot = dqget(sb, id, type);
  2093. if (!dquot)
  2094. return -ESRCH;
  2095. do_get_dqblk(dquot, di);
  2096. dqput(dquot);
  2097. return 0;
  2098. }
  2099. EXPORT_SYMBOL(dquot_get_dqblk);
  2100. #define VFS_FS_DQ_MASK \
  2101. (FS_DQ_BCOUNT | FS_DQ_BSOFT | FS_DQ_BHARD | \
  2102. FS_DQ_ICOUNT | FS_DQ_ISOFT | FS_DQ_IHARD | \
  2103. FS_DQ_BTIMER | FS_DQ_ITIMER)
  2104. /* Generic routine for setting common part of quota structure */
  2105. static int do_set_dqblk(struct dquot *dquot, struct fs_disk_quota *di)
  2106. {
  2107. struct mem_dqblk *dm = &dquot->dq_dqb;
  2108. int check_blim = 0, check_ilim = 0;
  2109. struct mem_dqinfo *dqi = &sb_dqopt(dquot->dq_sb)->info[dquot->dq_type];
  2110. if (di->d_fieldmask & ~VFS_FS_DQ_MASK)
  2111. return -EINVAL;
  2112. if (((di->d_fieldmask & FS_DQ_BSOFT) &&
  2113. (di->d_blk_softlimit > dqi->dqi_maxblimit)) ||
  2114. ((di->d_fieldmask & FS_DQ_BHARD) &&
  2115. (di->d_blk_hardlimit > dqi->dqi_maxblimit)) ||
  2116. ((di->d_fieldmask & FS_DQ_ISOFT) &&
  2117. (di->d_ino_softlimit > dqi->dqi_maxilimit)) ||
  2118. ((di->d_fieldmask & FS_DQ_IHARD) &&
  2119. (di->d_ino_hardlimit > dqi->dqi_maxilimit)))
  2120. return -ERANGE;
  2121. spin_lock(&dq_data_lock);
  2122. if (di->d_fieldmask & FS_DQ_BCOUNT) {
  2123. dm->dqb_curspace = di->d_bcount - dm->dqb_rsvspace;
  2124. check_blim = 1;
  2125. set_bit(DQ_LASTSET_B + QIF_SPACE_B, &dquot->dq_flags);
  2126. }
  2127. if (di->d_fieldmask & FS_DQ_BSOFT)
  2128. dm->dqb_bsoftlimit = qbtos(di->d_blk_softlimit);
  2129. if (di->d_fieldmask & FS_DQ_BHARD)
  2130. dm->dqb_bhardlimit = qbtos(di->d_blk_hardlimit);
  2131. if (di->d_fieldmask & (FS_DQ_BSOFT | FS_DQ_BHARD)) {
  2132. check_blim = 1;
  2133. set_bit(DQ_LASTSET_B + QIF_BLIMITS_B, &dquot->dq_flags);
  2134. }
  2135. if (di->d_fieldmask & FS_DQ_ICOUNT) {
  2136. dm->dqb_curinodes = di->d_icount;
  2137. check_ilim = 1;
  2138. set_bit(DQ_LASTSET_B + QIF_INODES_B, &dquot->dq_flags);
  2139. }
  2140. if (di->d_fieldmask & FS_DQ_ISOFT)
  2141. dm->dqb_isoftlimit = di->d_ino_softlimit;
  2142. if (di->d_fieldmask & FS_DQ_IHARD)
  2143. dm->dqb_ihardlimit = di->d_ino_hardlimit;
  2144. if (di->d_fieldmask & (FS_DQ_ISOFT | FS_DQ_IHARD)) {
  2145. check_ilim = 1;
  2146. set_bit(DQ_LASTSET_B + QIF_ILIMITS_B, &dquot->dq_flags);
  2147. }
  2148. if (di->d_fieldmask & FS_DQ_BTIMER) {
  2149. dm->dqb_btime = di->d_btimer;
  2150. check_blim = 1;
  2151. set_bit(DQ_LASTSET_B + QIF_BTIME_B, &dquot->dq_flags);
  2152. }
  2153. if (di->d_fieldmask & FS_DQ_ITIMER) {
  2154. dm->dqb_itime = di->d_itimer;
  2155. check_ilim = 1;
  2156. set_bit(DQ_LASTSET_B + QIF_ITIME_B, &dquot->dq_flags);
  2157. }
  2158. if (check_blim) {
  2159. if (!dm->dqb_bsoftlimit ||
  2160. dm->dqb_curspace < dm->dqb_bsoftlimit) {
  2161. dm->dqb_btime = 0;
  2162. clear_bit(DQ_BLKS_B, &dquot->dq_flags);
  2163. } else if (!(di->d_fieldmask & FS_DQ_BTIMER))
  2164. /* Set grace only if user hasn't provided his own... */
  2165. dm->dqb_btime = get_seconds() + dqi->dqi_bgrace;
  2166. }
  2167. if (check_ilim) {
  2168. if (!dm->dqb_isoftlimit ||
  2169. dm->dqb_curinodes < dm->dqb_isoftlimit) {
  2170. dm->dqb_itime = 0;
  2171. clear_bit(DQ_INODES_B, &dquot->dq_flags);
  2172. } else if (!(di->d_fieldmask & FS_DQ_ITIMER))
  2173. /* Set grace only if user hasn't provided his own... */
  2174. dm->dqb_itime = get_seconds() + dqi->dqi_igrace;
  2175. }
  2176. if (dm->dqb_bhardlimit || dm->dqb_bsoftlimit || dm->dqb_ihardlimit ||
  2177. dm->dqb_isoftlimit)
  2178. clear_bit(DQ_FAKE_B, &dquot->dq_flags);
  2179. else
  2180. set_bit(DQ_FAKE_B, &dquot->dq_flags);
  2181. spin_unlock(&dq_data_lock);
  2182. mark_dquot_dirty(dquot);
  2183. return 0;
  2184. }
  2185. int dquot_set_dqblk(struct super_block *sb, int type, qid_t id,
  2186. struct fs_disk_quota *di)
  2187. {
  2188. struct dquot *dquot;
  2189. int rc;
  2190. dquot = dqget(sb, id, type);
  2191. if (!dquot) {
  2192. rc = -ESRCH;
  2193. goto out;
  2194. }
  2195. rc = do_set_dqblk(dquot, di);
  2196. dqput(dquot);
  2197. out:
  2198. return rc;
  2199. }
  2200. EXPORT_SYMBOL(dquot_set_dqblk);
  2201. /* Generic routine for getting common part of quota file information */
  2202. int dquot_get_dqinfo(struct super_block *sb, int type, struct if_dqinfo *ii)
  2203. {
  2204. struct mem_dqinfo *mi;
  2205. mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
  2206. if (!sb_has_quota_active(sb, type)) {
  2207. mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
  2208. return -ESRCH;
  2209. }
  2210. mi = sb_dqopt(sb)->info + type;
  2211. spin_lock(&dq_data_lock);
  2212. ii->dqi_bgrace = mi->dqi_bgrace;
  2213. ii->dqi_igrace = mi->dqi_igrace;
  2214. ii->dqi_flags = mi->dqi_flags & DQF_MASK;
  2215. ii->dqi_valid = IIF_ALL;
  2216. spin_unlock(&dq_data_lock);
  2217. mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
  2218. return 0;
  2219. }
  2220. EXPORT_SYMBOL(dquot_get_dqinfo);
  2221. /* Generic routine for setting common part of quota file information */
  2222. int dquot_set_dqinfo(struct super_block *sb, int type, struct if_dqinfo *ii)
  2223. {
  2224. struct mem_dqinfo *mi;
  2225. int err = 0;
  2226. mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
  2227. if (!sb_has_quota_active(sb, type)) {
  2228. err = -ESRCH;
  2229. goto out;
  2230. }
  2231. mi = sb_dqopt(sb)->info + type;
  2232. spin_lock(&dq_data_lock);
  2233. if (ii->dqi_valid & IIF_BGRACE)
  2234. mi->dqi_bgrace = ii->dqi_bgrace;
  2235. if (ii->dqi_valid & IIF_IGRACE)
  2236. mi->dqi_igrace = ii->dqi_igrace;
  2237. if (ii->dqi_valid & IIF_FLAGS)
  2238. mi->dqi_flags = (mi->dqi_flags & ~DQF_MASK) |
  2239. (ii->dqi_flags & DQF_MASK);
  2240. spin_unlock(&dq_data_lock);
  2241. mark_info_dirty(sb, type);
  2242. /* Force write to disk */
  2243. sb->dq_op->write_info(sb, type);
  2244. out:
  2245. mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
  2246. return err;
  2247. }
  2248. EXPORT_SYMBOL(dquot_set_dqinfo);
  2249. const struct quotactl_ops dquot_quotactl_ops = {
  2250. .quota_on = dquot_quota_on,
  2251. .quota_off = dquot_quota_off,
  2252. .quota_sync = dquot_quota_sync,
  2253. .get_info = dquot_get_dqinfo,
  2254. .set_info = dquot_set_dqinfo,
  2255. .get_dqblk = dquot_get_dqblk,
  2256. .set_dqblk = dquot_set_dqblk
  2257. };
  2258. EXPORT_SYMBOL(dquot_quotactl_ops);
  2259. static int do_proc_dqstats(struct ctl_table *table, int write,
  2260. void __user *buffer, size_t *lenp, loff_t *ppos)
  2261. {
  2262. unsigned int type = (int *)table->data - dqstats.stat;
  2263. /* Update global table */
  2264. dqstats.stat[type] =
  2265. percpu_counter_sum_positive(&dqstats.counter[type]);
  2266. return proc_dointvec(table, write, buffer, lenp, ppos);
  2267. }
  2268. static ctl_table fs_dqstats_table[] = {
  2269. {
  2270. .procname = "lookups",
  2271. .data = &dqstats.stat[DQST_LOOKUPS],
  2272. .maxlen = sizeof(int),
  2273. .mode = 0444,
  2274. .proc_handler = do_proc_dqstats,
  2275. },
  2276. {
  2277. .procname = "drops",
  2278. .data = &dqstats.stat[DQST_DROPS],
  2279. .maxlen = sizeof(int),
  2280. .mode = 0444,
  2281. .proc_handler = do_proc_dqstats,
  2282. },
  2283. {
  2284. .procname = "reads",
  2285. .data = &dqstats.stat[DQST_READS],
  2286. .maxlen = sizeof(int),
  2287. .mode = 0444,
  2288. .proc_handler = do_proc_dqstats,
  2289. },
  2290. {
  2291. .procname = "writes",
  2292. .data = &dqstats.stat[DQST_WRITES],
  2293. .maxlen = sizeof(int),
  2294. .mode = 0444,
  2295. .proc_handler = do_proc_dqstats,
  2296. },
  2297. {
  2298. .procname = "cache_hits",
  2299. .data = &dqstats.stat[DQST_CACHE_HITS],
  2300. .maxlen = sizeof(int),
  2301. .mode = 0444,
  2302. .proc_handler = do_proc_dqstats,
  2303. },
  2304. {
  2305. .procname = "allocated_dquots",
  2306. .data = &dqstats.stat[DQST_ALLOC_DQUOTS],
  2307. .maxlen = sizeof(int),
  2308. .mode = 0444,
  2309. .proc_handler = do_proc_dqstats,
  2310. },
  2311. {
  2312. .procname = "free_dquots",
  2313. .data = &dqstats.stat[DQST_FREE_DQUOTS],
  2314. .maxlen = sizeof(int),
  2315. .mode = 0444,
  2316. .proc_handler = do_proc_dqstats,
  2317. },
  2318. {
  2319. .procname = "syncs",
  2320. .data = &dqstats.stat[DQST_SYNCS],
  2321. .maxlen = sizeof(int),
  2322. .mode = 0444,
  2323. .proc_handler = do_proc_dqstats,
  2324. },
  2325. #ifdef CONFIG_PRINT_QUOTA_WARNING
  2326. {
  2327. .procname = "warnings",
  2328. .data = &flag_print_warnings,
  2329. .maxlen = sizeof(int),
  2330. .mode = 0644,
  2331. .proc_handler = proc_dointvec,
  2332. },
  2333. #endif
  2334. { },
  2335. };
  2336. static ctl_table fs_table[] = {
  2337. {
  2338. .procname = "quota",
  2339. .mode = 0555,
  2340. .child = fs_dqstats_table,
  2341. },
  2342. { },
  2343. };
  2344. static ctl_table sys_table[] = {
  2345. {
  2346. .procname = "fs",
  2347. .mode = 0555,
  2348. .child = fs_table,
  2349. },
  2350. { },
  2351. };
  2352. static int __init dquot_init(void)
  2353. {
  2354. int i, ret;
  2355. unsigned long nr_hash, order;
  2356. printk(KERN_NOTICE "VFS: Disk quotas %s\n", __DQUOT_VERSION__);
  2357. register_sysctl_table(sys_table);
  2358. dquot_cachep = kmem_cache_create("dquot",
  2359. sizeof(struct dquot), sizeof(unsigned long) * 4,
  2360. (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
  2361. SLAB_MEM_SPREAD|SLAB_PANIC),
  2362. NULL);
  2363. order = 0;
  2364. dquot_hash = (struct hlist_head *)__get_free_pages(GFP_ATOMIC, order);
  2365. if (!dquot_hash)
  2366. panic("Cannot create dquot hash table");
  2367. for (i = 0; i < _DQST_DQSTAT_LAST; i++) {
  2368. ret = percpu_counter_init(&dqstats.counter[i], 0);
  2369. if (ret)
  2370. panic("Cannot create dquot stat counters");
  2371. }
  2372. /* Find power-of-two hlist_heads which can fit into allocation */
  2373. nr_hash = (1UL << order) * PAGE_SIZE / sizeof(struct hlist_head);
  2374. dq_hash_bits = 0;
  2375. do {
  2376. dq_hash_bits++;
  2377. } while (nr_hash >> dq_hash_bits);
  2378. dq_hash_bits--;
  2379. nr_hash = 1UL << dq_hash_bits;
  2380. dq_hash_mask = nr_hash - 1;
  2381. for (i = 0; i < nr_hash; i++)
  2382. INIT_HLIST_HEAD(dquot_hash + i);
  2383. printk("Dquot-cache hash table entries: %ld (order %ld, %ld bytes)\n",
  2384. nr_hash, order, (PAGE_SIZE << order));
  2385. register_shrinker(&dqcache_shrinker);
  2386. return 0;
  2387. }
  2388. module_init(dquot_init);