dquot.c 70 KB

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