dquot.c 72 KB

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