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