dquot.c 70 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591
  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. int reserved = 0;
  797. spin_lock(&inode_lock);
  798. list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
  799. if (inode->i_state & (I_FREEING|I_CLEAR|I_WILL_FREE|I_NEW))
  800. continue;
  801. if (unlikely(inode_get_rsv_space(inode) > 0))
  802. reserved = 1;
  803. if (!atomic_read(&inode->i_writecount))
  804. continue;
  805. if (!dqinit_needed(inode, type))
  806. continue;
  807. __iget(inode);
  808. spin_unlock(&inode_lock);
  809. iput(old_inode);
  810. __dquot_initialize(inode, type);
  811. /* We hold a reference to 'inode' so it couldn't have been
  812. * removed from s_inodes list while we dropped the inode_lock.
  813. * We cannot iput the inode now as we can be holding the last
  814. * reference and we cannot iput it under inode_lock. So we
  815. * keep the reference and iput it later. */
  816. old_inode = inode;
  817. spin_lock(&inode_lock);
  818. }
  819. spin_unlock(&inode_lock);
  820. iput(old_inode);
  821. if (reserved) {
  822. printk(KERN_WARNING "VFS (%s): Writes happened before quota"
  823. " was turned on thus quota information is probably "
  824. "inconsistent. Please run quotacheck(8).\n", sb->s_id);
  825. }
  826. }
  827. /*
  828. * Return 0 if dqput() won't block.
  829. * (note that 1 doesn't necessarily mean blocking)
  830. */
  831. static inline int dqput_blocks(struct dquot *dquot)
  832. {
  833. if (atomic_read(&dquot->dq_count) <= 1)
  834. return 1;
  835. return 0;
  836. }
  837. /*
  838. * Remove references to dquots from inode and add dquot to list for freeing
  839. * if we have the last referece to dquot
  840. * We can't race with anybody because we hold dqptr_sem for writing...
  841. */
  842. static int remove_inode_dquot_ref(struct inode *inode, int type,
  843. struct list_head *tofree_head)
  844. {
  845. struct dquot *dquot = inode->i_dquot[type];
  846. inode->i_dquot[type] = NULL;
  847. if (dquot) {
  848. if (dqput_blocks(dquot)) {
  849. #ifdef __DQUOT_PARANOIA
  850. if (atomic_read(&dquot->dq_count) != 1)
  851. printk(KERN_WARNING "VFS: Adding dquot with dq_count %d to dispose list.\n", atomic_read(&dquot->dq_count));
  852. #endif
  853. spin_lock(&dq_list_lock);
  854. /* As dquot must have currently users it can't be on
  855. * the free list... */
  856. list_add(&dquot->dq_free, tofree_head);
  857. spin_unlock(&dq_list_lock);
  858. return 1;
  859. }
  860. else
  861. dqput(dquot); /* We have guaranteed we won't block */
  862. }
  863. return 0;
  864. }
  865. /*
  866. * Free list of dquots
  867. * Dquots are removed from inodes and no new references can be got so we are
  868. * the only ones holding reference
  869. */
  870. static void put_dquot_list(struct list_head *tofree_head)
  871. {
  872. struct list_head *act_head;
  873. struct dquot *dquot;
  874. act_head = tofree_head->next;
  875. while (act_head != tofree_head) {
  876. dquot = list_entry(act_head, struct dquot, dq_free);
  877. act_head = act_head->next;
  878. /* Remove dquot from the list so we won't have problems... */
  879. list_del_init(&dquot->dq_free);
  880. dqput(dquot);
  881. }
  882. }
  883. static void remove_dquot_ref(struct super_block *sb, int type,
  884. struct list_head *tofree_head)
  885. {
  886. struct inode *inode;
  887. spin_lock(&inode_lock);
  888. list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
  889. /*
  890. * We have to scan also I_NEW inodes because they can already
  891. * have quota pointer initialized. Luckily, we need to touch
  892. * only quota pointers and these have separate locking
  893. * (dqptr_sem).
  894. */
  895. if (!IS_NOQUOTA(inode))
  896. remove_inode_dquot_ref(inode, type, tofree_head);
  897. }
  898. spin_unlock(&inode_lock);
  899. }
  900. /* Gather all references from inodes and drop them */
  901. static void drop_dquot_ref(struct super_block *sb, int type)
  902. {
  903. LIST_HEAD(tofree_head);
  904. if (sb->dq_op) {
  905. down_write(&sb_dqopt(sb)->dqptr_sem);
  906. remove_dquot_ref(sb, type, &tofree_head);
  907. up_write(&sb_dqopt(sb)->dqptr_sem);
  908. put_dquot_list(&tofree_head);
  909. }
  910. }
  911. static inline void dquot_incr_inodes(struct dquot *dquot, qsize_t number)
  912. {
  913. dquot->dq_dqb.dqb_curinodes += number;
  914. }
  915. static inline void dquot_incr_space(struct dquot *dquot, qsize_t number)
  916. {
  917. dquot->dq_dqb.dqb_curspace += number;
  918. }
  919. static inline void dquot_resv_space(struct dquot *dquot, qsize_t number)
  920. {
  921. dquot->dq_dqb.dqb_rsvspace += number;
  922. }
  923. /*
  924. * Claim reserved quota space
  925. */
  926. static void dquot_claim_reserved_space(struct dquot *dquot, qsize_t number)
  927. {
  928. if (dquot->dq_dqb.dqb_rsvspace < number) {
  929. WARN_ON_ONCE(1);
  930. number = dquot->dq_dqb.dqb_rsvspace;
  931. }
  932. dquot->dq_dqb.dqb_curspace += number;
  933. dquot->dq_dqb.dqb_rsvspace -= number;
  934. }
  935. static inline
  936. void dquot_free_reserved_space(struct dquot *dquot, qsize_t number)
  937. {
  938. if (dquot->dq_dqb.dqb_rsvspace >= number)
  939. dquot->dq_dqb.dqb_rsvspace -= number;
  940. else {
  941. WARN_ON_ONCE(1);
  942. dquot->dq_dqb.dqb_rsvspace = 0;
  943. }
  944. }
  945. static void dquot_decr_inodes(struct dquot *dquot, qsize_t number)
  946. {
  947. if (sb_dqopt(dquot->dq_sb)->flags & DQUOT_NEGATIVE_USAGE ||
  948. dquot->dq_dqb.dqb_curinodes >= number)
  949. dquot->dq_dqb.dqb_curinodes -= number;
  950. else
  951. dquot->dq_dqb.dqb_curinodes = 0;
  952. if (dquot->dq_dqb.dqb_curinodes <= dquot->dq_dqb.dqb_isoftlimit)
  953. dquot->dq_dqb.dqb_itime = (time_t) 0;
  954. clear_bit(DQ_INODES_B, &dquot->dq_flags);
  955. }
  956. static void dquot_decr_space(struct dquot *dquot, qsize_t number)
  957. {
  958. if (sb_dqopt(dquot->dq_sb)->flags & DQUOT_NEGATIVE_USAGE ||
  959. dquot->dq_dqb.dqb_curspace >= number)
  960. dquot->dq_dqb.dqb_curspace -= number;
  961. else
  962. dquot->dq_dqb.dqb_curspace = 0;
  963. if (dquot->dq_dqb.dqb_curspace <= dquot->dq_dqb.dqb_bsoftlimit)
  964. dquot->dq_dqb.dqb_btime = (time_t) 0;
  965. clear_bit(DQ_BLKS_B, &dquot->dq_flags);
  966. }
  967. static int warning_issued(struct dquot *dquot, const int warntype)
  968. {
  969. int flag = (warntype == QUOTA_NL_BHARDWARN ||
  970. warntype == QUOTA_NL_BSOFTLONGWARN) ? DQ_BLKS_B :
  971. ((warntype == QUOTA_NL_IHARDWARN ||
  972. warntype == QUOTA_NL_ISOFTLONGWARN) ? DQ_INODES_B : 0);
  973. if (!flag)
  974. return 0;
  975. return test_and_set_bit(flag, &dquot->dq_flags);
  976. }
  977. #ifdef CONFIG_PRINT_QUOTA_WARNING
  978. static int flag_print_warnings = 1;
  979. static int need_print_warning(struct dquot *dquot)
  980. {
  981. if (!flag_print_warnings)
  982. return 0;
  983. switch (dquot->dq_type) {
  984. case USRQUOTA:
  985. return current_fsuid() == dquot->dq_id;
  986. case GRPQUOTA:
  987. return in_group_p(dquot->dq_id);
  988. }
  989. return 0;
  990. }
  991. /* Print warning to user which exceeded quota */
  992. static void print_warning(struct dquot *dquot, const int warntype)
  993. {
  994. char *msg = NULL;
  995. struct tty_struct *tty;
  996. if (warntype == QUOTA_NL_IHARDBELOW ||
  997. warntype == QUOTA_NL_ISOFTBELOW ||
  998. warntype == QUOTA_NL_BHARDBELOW ||
  999. warntype == QUOTA_NL_BSOFTBELOW || !need_print_warning(dquot))
  1000. return;
  1001. tty = get_current_tty();
  1002. if (!tty)
  1003. return;
  1004. tty_write_message(tty, dquot->dq_sb->s_id);
  1005. if (warntype == QUOTA_NL_ISOFTWARN || warntype == QUOTA_NL_BSOFTWARN)
  1006. tty_write_message(tty, ": warning, ");
  1007. else
  1008. tty_write_message(tty, ": write failed, ");
  1009. tty_write_message(tty, quotatypes[dquot->dq_type]);
  1010. switch (warntype) {
  1011. case QUOTA_NL_IHARDWARN:
  1012. msg = " file limit reached.\r\n";
  1013. break;
  1014. case QUOTA_NL_ISOFTLONGWARN:
  1015. msg = " file quota exceeded too long.\r\n";
  1016. break;
  1017. case QUOTA_NL_ISOFTWARN:
  1018. msg = " file quota exceeded.\r\n";
  1019. break;
  1020. case QUOTA_NL_BHARDWARN:
  1021. msg = " block limit reached.\r\n";
  1022. break;
  1023. case QUOTA_NL_BSOFTLONGWARN:
  1024. msg = " block quota exceeded too long.\r\n";
  1025. break;
  1026. case QUOTA_NL_BSOFTWARN:
  1027. msg = " block quota exceeded.\r\n";
  1028. break;
  1029. }
  1030. tty_write_message(tty, msg);
  1031. tty_kref_put(tty);
  1032. }
  1033. #endif
  1034. /*
  1035. * Write warnings to the console and send warning messages over netlink.
  1036. *
  1037. * Note that this function can sleep.
  1038. */
  1039. static void flush_warnings(struct dquot *const *dquots, char *warntype)
  1040. {
  1041. struct dquot *dq;
  1042. int i;
  1043. for (i = 0; i < MAXQUOTAS; i++) {
  1044. dq = dquots[i];
  1045. if (dq && warntype[i] != QUOTA_NL_NOWARN &&
  1046. !warning_issued(dq, warntype[i])) {
  1047. #ifdef CONFIG_PRINT_QUOTA_WARNING
  1048. print_warning(dq, warntype[i]);
  1049. #endif
  1050. quota_send_warning(dq->dq_type, dq->dq_id,
  1051. dq->dq_sb->s_dev, warntype[i]);
  1052. }
  1053. }
  1054. }
  1055. static int ignore_hardlimit(struct dquot *dquot)
  1056. {
  1057. struct mem_dqinfo *info = &sb_dqopt(dquot->dq_sb)->info[dquot->dq_type];
  1058. return capable(CAP_SYS_RESOURCE) &&
  1059. (info->dqi_format->qf_fmt_id != QFMT_VFS_OLD ||
  1060. !(info->dqi_flags & V1_DQF_RSQUASH));
  1061. }
  1062. /* needs dq_data_lock */
  1063. static int check_idq(struct dquot *dquot, qsize_t inodes, char *warntype)
  1064. {
  1065. qsize_t newinodes = dquot->dq_dqb.dqb_curinodes + inodes;
  1066. *warntype = QUOTA_NL_NOWARN;
  1067. if (!sb_has_quota_limits_enabled(dquot->dq_sb, dquot->dq_type) ||
  1068. test_bit(DQ_FAKE_B, &dquot->dq_flags))
  1069. return 0;
  1070. if (dquot->dq_dqb.dqb_ihardlimit &&
  1071. newinodes > dquot->dq_dqb.dqb_ihardlimit &&
  1072. !ignore_hardlimit(dquot)) {
  1073. *warntype = QUOTA_NL_IHARDWARN;
  1074. return -EDQUOT;
  1075. }
  1076. if (dquot->dq_dqb.dqb_isoftlimit &&
  1077. newinodes > dquot->dq_dqb.dqb_isoftlimit &&
  1078. dquot->dq_dqb.dqb_itime &&
  1079. get_seconds() >= dquot->dq_dqb.dqb_itime &&
  1080. !ignore_hardlimit(dquot)) {
  1081. *warntype = QUOTA_NL_ISOFTLONGWARN;
  1082. return -EDQUOT;
  1083. }
  1084. if (dquot->dq_dqb.dqb_isoftlimit &&
  1085. newinodes > dquot->dq_dqb.dqb_isoftlimit &&
  1086. dquot->dq_dqb.dqb_itime == 0) {
  1087. *warntype = QUOTA_NL_ISOFTWARN;
  1088. dquot->dq_dqb.dqb_itime = get_seconds() +
  1089. sb_dqopt(dquot->dq_sb)->info[dquot->dq_type].dqi_igrace;
  1090. }
  1091. return 0;
  1092. }
  1093. /* needs dq_data_lock */
  1094. static int check_bdq(struct dquot *dquot, qsize_t space, int prealloc, char *warntype)
  1095. {
  1096. qsize_t tspace;
  1097. struct super_block *sb = dquot->dq_sb;
  1098. *warntype = QUOTA_NL_NOWARN;
  1099. if (!sb_has_quota_limits_enabled(sb, dquot->dq_type) ||
  1100. test_bit(DQ_FAKE_B, &dquot->dq_flags))
  1101. return 0;
  1102. tspace = dquot->dq_dqb.dqb_curspace + dquot->dq_dqb.dqb_rsvspace
  1103. + space;
  1104. if (dquot->dq_dqb.dqb_bhardlimit &&
  1105. tspace > dquot->dq_dqb.dqb_bhardlimit &&
  1106. !ignore_hardlimit(dquot)) {
  1107. if (!prealloc)
  1108. *warntype = QUOTA_NL_BHARDWARN;
  1109. return -EDQUOT;
  1110. }
  1111. if (dquot->dq_dqb.dqb_bsoftlimit &&
  1112. tspace > dquot->dq_dqb.dqb_bsoftlimit &&
  1113. dquot->dq_dqb.dqb_btime &&
  1114. get_seconds() >= dquot->dq_dqb.dqb_btime &&
  1115. !ignore_hardlimit(dquot)) {
  1116. if (!prealloc)
  1117. *warntype = QUOTA_NL_BSOFTLONGWARN;
  1118. return -EDQUOT;
  1119. }
  1120. if (dquot->dq_dqb.dqb_bsoftlimit &&
  1121. tspace > dquot->dq_dqb.dqb_bsoftlimit &&
  1122. dquot->dq_dqb.dqb_btime == 0) {
  1123. if (!prealloc) {
  1124. *warntype = QUOTA_NL_BSOFTWARN;
  1125. dquot->dq_dqb.dqb_btime = get_seconds() +
  1126. sb_dqopt(sb)->info[dquot->dq_type].dqi_bgrace;
  1127. }
  1128. else
  1129. /*
  1130. * We don't allow preallocation to exceed softlimit so exceeding will
  1131. * be always printed
  1132. */
  1133. return -EDQUOT;
  1134. }
  1135. return 0;
  1136. }
  1137. static int info_idq_free(struct dquot *dquot, qsize_t inodes)
  1138. {
  1139. qsize_t newinodes;
  1140. if (test_bit(DQ_FAKE_B, &dquot->dq_flags) ||
  1141. dquot->dq_dqb.dqb_curinodes <= dquot->dq_dqb.dqb_isoftlimit ||
  1142. !sb_has_quota_limits_enabled(dquot->dq_sb, dquot->dq_type))
  1143. return QUOTA_NL_NOWARN;
  1144. newinodes = dquot->dq_dqb.dqb_curinodes - inodes;
  1145. if (newinodes <= dquot->dq_dqb.dqb_isoftlimit)
  1146. return QUOTA_NL_ISOFTBELOW;
  1147. if (dquot->dq_dqb.dqb_curinodes >= dquot->dq_dqb.dqb_ihardlimit &&
  1148. newinodes < dquot->dq_dqb.dqb_ihardlimit)
  1149. return QUOTA_NL_IHARDBELOW;
  1150. return QUOTA_NL_NOWARN;
  1151. }
  1152. static int info_bdq_free(struct dquot *dquot, qsize_t space)
  1153. {
  1154. if (test_bit(DQ_FAKE_B, &dquot->dq_flags) ||
  1155. dquot->dq_dqb.dqb_curspace <= dquot->dq_dqb.dqb_bsoftlimit)
  1156. return QUOTA_NL_NOWARN;
  1157. if (dquot->dq_dqb.dqb_curspace - space <= dquot->dq_dqb.dqb_bsoftlimit)
  1158. return QUOTA_NL_BSOFTBELOW;
  1159. if (dquot->dq_dqb.dqb_curspace >= dquot->dq_dqb.dqb_bhardlimit &&
  1160. dquot->dq_dqb.dqb_curspace - space < dquot->dq_dqb.dqb_bhardlimit)
  1161. return QUOTA_NL_BHARDBELOW;
  1162. return QUOTA_NL_NOWARN;
  1163. }
  1164. /*
  1165. * Initialize quota pointers in inode
  1166. *
  1167. * We do things in a bit complicated way but by that we avoid calling
  1168. * dqget() and thus filesystem callbacks under dqptr_sem.
  1169. *
  1170. * It is better to call this function outside of any transaction as it
  1171. * might need a lot of space in journal for dquot structure allocation.
  1172. */
  1173. static void __dquot_initialize(struct inode *inode, int type)
  1174. {
  1175. unsigned int id = 0;
  1176. int cnt;
  1177. struct dquot *got[MAXQUOTAS];
  1178. struct super_block *sb = inode->i_sb;
  1179. qsize_t rsv;
  1180. /* First test before acquiring mutex - solves deadlocks when we
  1181. * re-enter the quota code and are already holding the mutex */
  1182. if (!sb_any_quota_active(inode->i_sb) || IS_NOQUOTA(inode))
  1183. return;
  1184. /* First get references to structures we might need. */
  1185. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1186. got[cnt] = NULL;
  1187. if (type != -1 && cnt != type)
  1188. continue;
  1189. switch (cnt) {
  1190. case USRQUOTA:
  1191. id = inode->i_uid;
  1192. break;
  1193. case GRPQUOTA:
  1194. id = inode->i_gid;
  1195. break;
  1196. }
  1197. got[cnt] = dqget(sb, id, cnt);
  1198. }
  1199. down_write(&sb_dqopt(sb)->dqptr_sem);
  1200. if (IS_NOQUOTA(inode))
  1201. goto out_err;
  1202. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1203. if (type != -1 && cnt != type)
  1204. continue;
  1205. /* Avoid races with quotaoff() */
  1206. if (!sb_has_quota_active(sb, cnt))
  1207. continue;
  1208. if (!inode->i_dquot[cnt]) {
  1209. inode->i_dquot[cnt] = got[cnt];
  1210. got[cnt] = NULL;
  1211. /*
  1212. * Make quota reservation system happy if someone
  1213. * did a write before quota was turned on
  1214. */
  1215. rsv = inode_get_rsv_space(inode);
  1216. if (unlikely(rsv))
  1217. dquot_resv_space(inode->i_dquot[cnt], rsv);
  1218. }
  1219. }
  1220. out_err:
  1221. up_write(&sb_dqopt(sb)->dqptr_sem);
  1222. /* Drop unused references */
  1223. dqput_all(got);
  1224. }
  1225. void dquot_initialize(struct inode *inode)
  1226. {
  1227. __dquot_initialize(inode, -1);
  1228. }
  1229. EXPORT_SYMBOL(dquot_initialize);
  1230. /*
  1231. * Release all quotas referenced by inode
  1232. */
  1233. static void __dquot_drop(struct inode *inode)
  1234. {
  1235. int cnt;
  1236. struct dquot *put[MAXQUOTAS];
  1237. down_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1238. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1239. put[cnt] = inode->i_dquot[cnt];
  1240. inode->i_dquot[cnt] = NULL;
  1241. }
  1242. up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1243. dqput_all(put);
  1244. }
  1245. void dquot_drop(struct inode *inode)
  1246. {
  1247. int cnt;
  1248. if (IS_NOQUOTA(inode))
  1249. return;
  1250. /*
  1251. * Test before calling to rule out calls from proc and such
  1252. * where we are not allowed to block. Note that this is
  1253. * actually reliable test even without the lock - the caller
  1254. * must assure that nobody can come after the DQUOT_DROP and
  1255. * add quota pointers back anyway.
  1256. */
  1257. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1258. if (inode->i_dquot[cnt])
  1259. break;
  1260. }
  1261. if (cnt < MAXQUOTAS)
  1262. __dquot_drop(inode);
  1263. }
  1264. EXPORT_SYMBOL(dquot_drop);
  1265. /*
  1266. * inode_reserved_space is managed internally by quota, and protected by
  1267. * i_lock similar to i_blocks+i_bytes.
  1268. */
  1269. static qsize_t *inode_reserved_space(struct inode * inode)
  1270. {
  1271. /* Filesystem must explicitly define it's own method in order to use
  1272. * quota reservation interface */
  1273. BUG_ON(!inode->i_sb->dq_op->get_reserved_space);
  1274. return inode->i_sb->dq_op->get_reserved_space(inode);
  1275. }
  1276. void inode_add_rsv_space(struct inode *inode, qsize_t number)
  1277. {
  1278. spin_lock(&inode->i_lock);
  1279. *inode_reserved_space(inode) += number;
  1280. spin_unlock(&inode->i_lock);
  1281. }
  1282. EXPORT_SYMBOL(inode_add_rsv_space);
  1283. void inode_claim_rsv_space(struct inode *inode, qsize_t number)
  1284. {
  1285. spin_lock(&inode->i_lock);
  1286. *inode_reserved_space(inode) -= number;
  1287. __inode_add_bytes(inode, number);
  1288. spin_unlock(&inode->i_lock);
  1289. }
  1290. EXPORT_SYMBOL(inode_claim_rsv_space);
  1291. void inode_sub_rsv_space(struct inode *inode, qsize_t number)
  1292. {
  1293. spin_lock(&inode->i_lock);
  1294. *inode_reserved_space(inode) -= number;
  1295. spin_unlock(&inode->i_lock);
  1296. }
  1297. EXPORT_SYMBOL(inode_sub_rsv_space);
  1298. static qsize_t inode_get_rsv_space(struct inode *inode)
  1299. {
  1300. qsize_t ret;
  1301. if (!inode->i_sb->dq_op->get_reserved_space)
  1302. return 0;
  1303. spin_lock(&inode->i_lock);
  1304. ret = *inode_reserved_space(inode);
  1305. spin_unlock(&inode->i_lock);
  1306. return ret;
  1307. }
  1308. static void inode_incr_space(struct inode *inode, qsize_t number,
  1309. int reserve)
  1310. {
  1311. if (reserve)
  1312. inode_add_rsv_space(inode, number);
  1313. else
  1314. inode_add_bytes(inode, number);
  1315. }
  1316. static void inode_decr_space(struct inode *inode, qsize_t number, int reserve)
  1317. {
  1318. if (reserve)
  1319. inode_sub_rsv_space(inode, number);
  1320. else
  1321. inode_sub_bytes(inode, number);
  1322. }
  1323. /*
  1324. * This functions updates i_blocks+i_bytes fields and quota information
  1325. * (together with appropriate checks).
  1326. *
  1327. * NOTE: We absolutely rely on the fact that caller dirties the inode
  1328. * (usually helpers in quotaops.h care about this) and holds a handle for
  1329. * the current transaction so that dquot write and inode write go into the
  1330. * same transaction.
  1331. */
  1332. /*
  1333. * This operation can block, but only after everything is updated
  1334. */
  1335. int __dquot_alloc_space(struct inode *inode, qsize_t number,
  1336. int warn, int reserve)
  1337. {
  1338. int cnt, ret = 0;
  1339. char warntype[MAXQUOTAS];
  1340. /*
  1341. * First test before acquiring mutex - solves deadlocks when we
  1342. * re-enter the quota code and are already holding the mutex
  1343. */
  1344. if (!sb_any_quota_active(inode->i_sb) || IS_NOQUOTA(inode)) {
  1345. inode_incr_space(inode, number, reserve);
  1346. goto out;
  1347. }
  1348. down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1349. for (cnt = 0; cnt < MAXQUOTAS; cnt++)
  1350. warntype[cnt] = QUOTA_NL_NOWARN;
  1351. spin_lock(&dq_data_lock);
  1352. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1353. if (!inode->i_dquot[cnt])
  1354. continue;
  1355. ret = check_bdq(inode->i_dquot[cnt], number, !warn,
  1356. warntype+cnt);
  1357. if (ret) {
  1358. spin_unlock(&dq_data_lock);
  1359. goto out_flush_warn;
  1360. }
  1361. }
  1362. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1363. if (!inode->i_dquot[cnt])
  1364. continue;
  1365. if (reserve)
  1366. dquot_resv_space(inode->i_dquot[cnt], number);
  1367. else
  1368. dquot_incr_space(inode->i_dquot[cnt], number);
  1369. }
  1370. inode_incr_space(inode, number, reserve);
  1371. spin_unlock(&dq_data_lock);
  1372. if (reserve)
  1373. goto out_flush_warn;
  1374. mark_all_dquot_dirty(inode->i_dquot);
  1375. out_flush_warn:
  1376. flush_warnings(inode->i_dquot, warntype);
  1377. up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1378. out:
  1379. return ret;
  1380. }
  1381. EXPORT_SYMBOL(__dquot_alloc_space);
  1382. /*
  1383. * This operation can block, but only after everything is updated
  1384. */
  1385. int dquot_alloc_inode(const struct inode *inode)
  1386. {
  1387. int cnt, ret = 0;
  1388. char warntype[MAXQUOTAS];
  1389. /* First test before acquiring mutex - solves deadlocks when we
  1390. * re-enter the quota code and are already holding the mutex */
  1391. if (!sb_any_quota_active(inode->i_sb) || IS_NOQUOTA(inode))
  1392. return 0;
  1393. for (cnt = 0; cnt < MAXQUOTAS; cnt++)
  1394. warntype[cnt] = QUOTA_NL_NOWARN;
  1395. down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1396. spin_lock(&dq_data_lock);
  1397. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1398. if (!inode->i_dquot[cnt])
  1399. continue;
  1400. ret = check_idq(inode->i_dquot[cnt], 1, warntype + cnt);
  1401. if (ret)
  1402. goto warn_put_all;
  1403. }
  1404. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1405. if (!inode->i_dquot[cnt])
  1406. continue;
  1407. dquot_incr_inodes(inode->i_dquot[cnt], 1);
  1408. }
  1409. warn_put_all:
  1410. spin_unlock(&dq_data_lock);
  1411. if (ret == 0)
  1412. mark_all_dquot_dirty(inode->i_dquot);
  1413. flush_warnings(inode->i_dquot, warntype);
  1414. up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1415. return ret;
  1416. }
  1417. EXPORT_SYMBOL(dquot_alloc_inode);
  1418. /*
  1419. * Convert in-memory reserved quotas to real consumed quotas
  1420. */
  1421. int dquot_claim_space_nodirty(struct inode *inode, qsize_t number)
  1422. {
  1423. int cnt;
  1424. if (!sb_any_quota_active(inode->i_sb) || IS_NOQUOTA(inode)) {
  1425. inode_claim_rsv_space(inode, number);
  1426. return 0;
  1427. }
  1428. down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1429. spin_lock(&dq_data_lock);
  1430. /* Claim reserved quotas to allocated quotas */
  1431. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1432. if (inode->i_dquot[cnt])
  1433. dquot_claim_reserved_space(inode->i_dquot[cnt],
  1434. number);
  1435. }
  1436. /* Update inode bytes */
  1437. inode_claim_rsv_space(inode, number);
  1438. spin_unlock(&dq_data_lock);
  1439. mark_all_dquot_dirty(inode->i_dquot);
  1440. up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1441. return 0;
  1442. }
  1443. EXPORT_SYMBOL(dquot_claim_space_nodirty);
  1444. /*
  1445. * This operation can block, but only after everything is updated
  1446. */
  1447. void __dquot_free_space(struct inode *inode, qsize_t number, int reserve)
  1448. {
  1449. unsigned int cnt;
  1450. char warntype[MAXQUOTAS];
  1451. /* First test before acquiring mutex - solves deadlocks when we
  1452. * re-enter the quota code and are already holding the mutex */
  1453. if (!sb_any_quota_active(inode->i_sb) || IS_NOQUOTA(inode)) {
  1454. inode_decr_space(inode, number, reserve);
  1455. return;
  1456. }
  1457. down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1458. spin_lock(&dq_data_lock);
  1459. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1460. if (!inode->i_dquot[cnt])
  1461. continue;
  1462. warntype[cnt] = info_bdq_free(inode->i_dquot[cnt], number);
  1463. if (reserve)
  1464. dquot_free_reserved_space(inode->i_dquot[cnt], number);
  1465. else
  1466. dquot_decr_space(inode->i_dquot[cnt], number);
  1467. }
  1468. inode_decr_space(inode, number, reserve);
  1469. spin_unlock(&dq_data_lock);
  1470. if (reserve)
  1471. goto out_unlock;
  1472. mark_all_dquot_dirty(inode->i_dquot);
  1473. out_unlock:
  1474. flush_warnings(inode->i_dquot, warntype);
  1475. up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1476. }
  1477. EXPORT_SYMBOL(__dquot_free_space);
  1478. /*
  1479. * This operation can block, but only after everything is updated
  1480. */
  1481. void dquot_free_inode(const struct inode *inode)
  1482. {
  1483. unsigned int cnt;
  1484. char warntype[MAXQUOTAS];
  1485. /* First test before acquiring mutex - solves deadlocks when we
  1486. * re-enter the quota code and are already holding the mutex */
  1487. if (!sb_any_quota_active(inode->i_sb) || IS_NOQUOTA(inode))
  1488. return;
  1489. down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1490. spin_lock(&dq_data_lock);
  1491. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1492. if (!inode->i_dquot[cnt])
  1493. continue;
  1494. warntype[cnt] = info_idq_free(inode->i_dquot[cnt], 1);
  1495. dquot_decr_inodes(inode->i_dquot[cnt], 1);
  1496. }
  1497. spin_unlock(&dq_data_lock);
  1498. mark_all_dquot_dirty(inode->i_dquot);
  1499. flush_warnings(inode->i_dquot, warntype);
  1500. up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1501. }
  1502. EXPORT_SYMBOL(dquot_free_inode);
  1503. /*
  1504. * Transfer the number of inode and blocks from one diskquota to an other.
  1505. *
  1506. * This operation can block, but only after everything is updated
  1507. * A transaction must be started when entering this function.
  1508. */
  1509. static int __dquot_transfer(struct inode *inode, qid_t *chid, unsigned long mask)
  1510. {
  1511. qsize_t space, cur_space;
  1512. qsize_t rsv_space = 0;
  1513. struct dquot *transfer_from[MAXQUOTAS];
  1514. struct dquot *transfer_to[MAXQUOTAS];
  1515. int cnt, ret = 0;
  1516. char warntype_to[MAXQUOTAS];
  1517. char warntype_from_inodes[MAXQUOTAS], warntype_from_space[MAXQUOTAS];
  1518. /* First test before acquiring mutex - solves deadlocks when we
  1519. * re-enter the quota code and are already holding the mutex */
  1520. if (IS_NOQUOTA(inode))
  1521. return 0;
  1522. /* Initialize the arrays */
  1523. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1524. transfer_from[cnt] = NULL;
  1525. transfer_to[cnt] = NULL;
  1526. warntype_to[cnt] = QUOTA_NL_NOWARN;
  1527. }
  1528. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1529. if (mask & (1 << cnt))
  1530. transfer_to[cnt] = dqget(inode->i_sb, chid[cnt], cnt);
  1531. }
  1532. down_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1533. if (IS_NOQUOTA(inode)) { /* File without quota accounting? */
  1534. up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1535. goto put_all;
  1536. }
  1537. spin_lock(&dq_data_lock);
  1538. cur_space = inode_get_bytes(inode);
  1539. rsv_space = inode_get_rsv_space(inode);
  1540. space = cur_space + rsv_space;
  1541. /* Build the transfer_from list and check the limits */
  1542. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1543. if (!transfer_to[cnt])
  1544. continue;
  1545. transfer_from[cnt] = inode->i_dquot[cnt];
  1546. ret = check_idq(transfer_to[cnt], 1, warntype_to + cnt);
  1547. if (ret)
  1548. goto over_quota;
  1549. ret = check_bdq(transfer_to[cnt], space, 0, warntype_to + cnt);
  1550. if (ret)
  1551. goto over_quota;
  1552. }
  1553. /*
  1554. * Finally perform the needed transfer from transfer_from to transfer_to
  1555. */
  1556. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1557. /*
  1558. * Skip changes for same uid or gid or for turned off quota-type.
  1559. */
  1560. if (!transfer_to[cnt])
  1561. continue;
  1562. /* Due to IO error we might not have transfer_from[] structure */
  1563. if (transfer_from[cnt]) {
  1564. warntype_from_inodes[cnt] =
  1565. info_idq_free(transfer_from[cnt], 1);
  1566. warntype_from_space[cnt] =
  1567. info_bdq_free(transfer_from[cnt], space);
  1568. dquot_decr_inodes(transfer_from[cnt], 1);
  1569. dquot_decr_space(transfer_from[cnt], cur_space);
  1570. dquot_free_reserved_space(transfer_from[cnt],
  1571. rsv_space);
  1572. }
  1573. dquot_incr_inodes(transfer_to[cnt], 1);
  1574. dquot_incr_space(transfer_to[cnt], cur_space);
  1575. dquot_resv_space(transfer_to[cnt], rsv_space);
  1576. inode->i_dquot[cnt] = transfer_to[cnt];
  1577. }
  1578. spin_unlock(&dq_data_lock);
  1579. up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1580. mark_all_dquot_dirty(transfer_from);
  1581. mark_all_dquot_dirty(transfer_to);
  1582. /* The reference we got is transferred to the inode */
  1583. for (cnt = 0; cnt < MAXQUOTAS; cnt++)
  1584. transfer_to[cnt] = NULL;
  1585. warn_put_all:
  1586. flush_warnings(transfer_to, warntype_to);
  1587. flush_warnings(transfer_from, warntype_from_inodes);
  1588. flush_warnings(transfer_from, warntype_from_space);
  1589. put_all:
  1590. dqput_all(transfer_from);
  1591. dqput_all(transfer_to);
  1592. return ret;
  1593. over_quota:
  1594. spin_unlock(&dq_data_lock);
  1595. up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1596. /* Clear dquot pointers we don't want to dqput() */
  1597. for (cnt = 0; cnt < MAXQUOTAS; cnt++)
  1598. transfer_from[cnt] = NULL;
  1599. goto warn_put_all;
  1600. }
  1601. /* Wrapper for transferring ownership of an inode for uid/gid only
  1602. * Called from FSXXX_setattr()
  1603. */
  1604. int dquot_transfer(struct inode *inode, struct iattr *iattr)
  1605. {
  1606. qid_t chid[MAXQUOTAS];
  1607. unsigned long mask = 0;
  1608. if (iattr->ia_valid & ATTR_UID && iattr->ia_uid != inode->i_uid) {
  1609. mask |= 1 << USRQUOTA;
  1610. chid[USRQUOTA] = iattr->ia_uid;
  1611. }
  1612. if (iattr->ia_valid & ATTR_GID && iattr->ia_gid != inode->i_gid) {
  1613. mask |= 1 << GRPQUOTA;
  1614. chid[GRPQUOTA] = iattr->ia_gid;
  1615. }
  1616. if (sb_any_quota_active(inode->i_sb) && !IS_NOQUOTA(inode)) {
  1617. dquot_initialize(inode);
  1618. return __dquot_transfer(inode, chid, mask);
  1619. }
  1620. return 0;
  1621. }
  1622. EXPORT_SYMBOL(dquot_transfer);
  1623. /*
  1624. * Write info of quota file to disk
  1625. */
  1626. int dquot_commit_info(struct super_block *sb, int type)
  1627. {
  1628. int ret;
  1629. struct quota_info *dqopt = sb_dqopt(sb);
  1630. mutex_lock(&dqopt->dqio_mutex);
  1631. ret = dqopt->ops[type]->write_file_info(sb, type);
  1632. mutex_unlock(&dqopt->dqio_mutex);
  1633. return ret;
  1634. }
  1635. EXPORT_SYMBOL(dquot_commit_info);
  1636. /*
  1637. * Definitions of diskquota operations.
  1638. */
  1639. const struct dquot_operations dquot_operations = {
  1640. .write_dquot = dquot_commit,
  1641. .acquire_dquot = dquot_acquire,
  1642. .release_dquot = dquot_release,
  1643. .mark_dirty = dquot_mark_dquot_dirty,
  1644. .write_info = dquot_commit_info,
  1645. .alloc_dquot = dquot_alloc,
  1646. .destroy_dquot = dquot_destroy,
  1647. };
  1648. /*
  1649. * Generic helper for ->open on filesystems supporting disk quotas.
  1650. */
  1651. int dquot_file_open(struct inode *inode, struct file *file)
  1652. {
  1653. int error;
  1654. error = generic_file_open(inode, file);
  1655. if (!error && (file->f_mode & FMODE_WRITE))
  1656. dquot_initialize(inode);
  1657. return error;
  1658. }
  1659. EXPORT_SYMBOL(dquot_file_open);
  1660. /*
  1661. * Turn quota off on a device. type == -1 ==> quotaoff for all types (umount)
  1662. */
  1663. int vfs_quota_disable(struct super_block *sb, int type, unsigned int flags)
  1664. {
  1665. int cnt, ret = 0;
  1666. struct quota_info *dqopt = sb_dqopt(sb);
  1667. struct inode *toputinode[MAXQUOTAS];
  1668. /* Cannot turn off usage accounting without turning off limits, or
  1669. * suspend quotas and simultaneously turn quotas off. */
  1670. if ((flags & DQUOT_USAGE_ENABLED && !(flags & DQUOT_LIMITS_ENABLED))
  1671. || (flags & DQUOT_SUSPENDED && flags & (DQUOT_LIMITS_ENABLED |
  1672. DQUOT_USAGE_ENABLED)))
  1673. return -EINVAL;
  1674. /* We need to serialize quota_off() for device */
  1675. mutex_lock(&dqopt->dqonoff_mutex);
  1676. /*
  1677. * Skip everything if there's nothing to do. We have to do this because
  1678. * sometimes we are called when fill_super() failed and calling
  1679. * sync_fs() in such cases does no good.
  1680. */
  1681. if (!sb_any_quota_loaded(sb)) {
  1682. mutex_unlock(&dqopt->dqonoff_mutex);
  1683. return 0;
  1684. }
  1685. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1686. toputinode[cnt] = NULL;
  1687. if (type != -1 && cnt != type)
  1688. continue;
  1689. if (!sb_has_quota_loaded(sb, cnt))
  1690. continue;
  1691. if (flags & DQUOT_SUSPENDED) {
  1692. spin_lock(&dq_state_lock);
  1693. dqopt->flags |=
  1694. dquot_state_flag(DQUOT_SUSPENDED, cnt);
  1695. spin_unlock(&dq_state_lock);
  1696. } else {
  1697. spin_lock(&dq_state_lock);
  1698. dqopt->flags &= ~dquot_state_flag(flags, cnt);
  1699. /* Turning off suspended quotas? */
  1700. if (!sb_has_quota_loaded(sb, cnt) &&
  1701. sb_has_quota_suspended(sb, cnt)) {
  1702. dqopt->flags &= ~dquot_state_flag(
  1703. DQUOT_SUSPENDED, cnt);
  1704. spin_unlock(&dq_state_lock);
  1705. iput(dqopt->files[cnt]);
  1706. dqopt->files[cnt] = NULL;
  1707. continue;
  1708. }
  1709. spin_unlock(&dq_state_lock);
  1710. }
  1711. /* We still have to keep quota loaded? */
  1712. if (sb_has_quota_loaded(sb, cnt) && !(flags & DQUOT_SUSPENDED))
  1713. continue;
  1714. /* Note: these are blocking operations */
  1715. drop_dquot_ref(sb, cnt);
  1716. invalidate_dquots(sb, cnt);
  1717. /*
  1718. * Now all dquots should be invalidated, all writes done so we
  1719. * should be only users of the info. No locks needed.
  1720. */
  1721. if (info_dirty(&dqopt->info[cnt]))
  1722. sb->dq_op->write_info(sb, cnt);
  1723. if (dqopt->ops[cnt]->free_file_info)
  1724. dqopt->ops[cnt]->free_file_info(sb, cnt);
  1725. put_quota_format(dqopt->info[cnt].dqi_format);
  1726. toputinode[cnt] = dqopt->files[cnt];
  1727. if (!sb_has_quota_loaded(sb, cnt))
  1728. dqopt->files[cnt] = NULL;
  1729. dqopt->info[cnt].dqi_flags = 0;
  1730. dqopt->info[cnt].dqi_igrace = 0;
  1731. dqopt->info[cnt].dqi_bgrace = 0;
  1732. dqopt->ops[cnt] = NULL;
  1733. }
  1734. mutex_unlock(&dqopt->dqonoff_mutex);
  1735. /* Skip syncing and setting flags if quota files are hidden */
  1736. if (dqopt->flags & DQUOT_QUOTA_SYS_FILE)
  1737. goto put_inodes;
  1738. /* Sync the superblock so that buffers with quota data are written to
  1739. * disk (and so userspace sees correct data afterwards). */
  1740. if (sb->s_op->sync_fs)
  1741. sb->s_op->sync_fs(sb, 1);
  1742. sync_blockdev(sb->s_bdev);
  1743. /* Now the quota files are just ordinary files and we can set the
  1744. * inode flags back. Moreover we discard the pagecache so that
  1745. * userspace sees the writes we did bypassing the pagecache. We
  1746. * must also discard the blockdev buffers so that we see the
  1747. * changes done by userspace on the next quotaon() */
  1748. for (cnt = 0; cnt < MAXQUOTAS; cnt++)
  1749. if (toputinode[cnt]) {
  1750. mutex_lock(&dqopt->dqonoff_mutex);
  1751. /* If quota was reenabled in the meantime, we have
  1752. * nothing to do */
  1753. if (!sb_has_quota_loaded(sb, cnt)) {
  1754. mutex_lock_nested(&toputinode[cnt]->i_mutex,
  1755. I_MUTEX_QUOTA);
  1756. toputinode[cnt]->i_flags &= ~(S_IMMUTABLE |
  1757. S_NOATIME | S_NOQUOTA);
  1758. truncate_inode_pages(&toputinode[cnt]->i_data,
  1759. 0);
  1760. mutex_unlock(&toputinode[cnt]->i_mutex);
  1761. mark_inode_dirty(toputinode[cnt]);
  1762. }
  1763. mutex_unlock(&dqopt->dqonoff_mutex);
  1764. }
  1765. if (sb->s_bdev)
  1766. invalidate_bdev(sb->s_bdev);
  1767. put_inodes:
  1768. for (cnt = 0; cnt < MAXQUOTAS; cnt++)
  1769. if (toputinode[cnt]) {
  1770. /* On remount RO, we keep the inode pointer so that we
  1771. * can reenable quota on the subsequent remount RW. We
  1772. * have to check 'flags' variable and not use sb_has_
  1773. * function because another quotaon / quotaoff could
  1774. * change global state before we got here. We refuse
  1775. * to suspend quotas when there is pending delete on
  1776. * the quota file... */
  1777. if (!(flags & DQUOT_SUSPENDED))
  1778. iput(toputinode[cnt]);
  1779. else if (!toputinode[cnt]->i_nlink)
  1780. ret = -EBUSY;
  1781. }
  1782. return ret;
  1783. }
  1784. EXPORT_SYMBOL(vfs_quota_disable);
  1785. int vfs_quota_off(struct super_block *sb, int type, int remount)
  1786. {
  1787. return vfs_quota_disable(sb, type, remount ? DQUOT_SUSPENDED :
  1788. (DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED));
  1789. }
  1790. EXPORT_SYMBOL(vfs_quota_off);
  1791. /*
  1792. * Turn quotas on on a device
  1793. */
  1794. /*
  1795. * Helper function to turn quotas on when we already have the inode of
  1796. * quota file and no quota information is loaded.
  1797. */
  1798. static int vfs_load_quota_inode(struct inode *inode, int type, int format_id,
  1799. unsigned int flags)
  1800. {
  1801. struct quota_format_type *fmt = find_quota_format(format_id);
  1802. struct super_block *sb = inode->i_sb;
  1803. struct quota_info *dqopt = sb_dqopt(sb);
  1804. int error;
  1805. int oldflags = -1;
  1806. if (!fmt)
  1807. return -ESRCH;
  1808. if (!S_ISREG(inode->i_mode)) {
  1809. error = -EACCES;
  1810. goto out_fmt;
  1811. }
  1812. if (IS_RDONLY(inode)) {
  1813. error = -EROFS;
  1814. goto out_fmt;
  1815. }
  1816. if (!sb->s_op->quota_write || !sb->s_op->quota_read) {
  1817. error = -EINVAL;
  1818. goto out_fmt;
  1819. }
  1820. /* Usage always has to be set... */
  1821. if (!(flags & DQUOT_USAGE_ENABLED)) {
  1822. error = -EINVAL;
  1823. goto out_fmt;
  1824. }
  1825. if (!(dqopt->flags & DQUOT_QUOTA_SYS_FILE)) {
  1826. /* As we bypass the pagecache we must now flush all the
  1827. * dirty data and invalidate caches so that kernel sees
  1828. * changes from userspace. It is not enough to just flush
  1829. * the quota file since if blocksize < pagesize, invalidation
  1830. * of the cache could fail because of other unrelated dirty
  1831. * data */
  1832. sync_filesystem(sb);
  1833. invalidate_bdev(sb->s_bdev);
  1834. }
  1835. mutex_lock(&dqopt->dqonoff_mutex);
  1836. if (sb_has_quota_loaded(sb, type)) {
  1837. error = -EBUSY;
  1838. goto out_lock;
  1839. }
  1840. if (!(dqopt->flags & DQUOT_QUOTA_SYS_FILE)) {
  1841. /* We don't want quota and atime on quota files (deadlocks
  1842. * possible) Also nobody should write to the file - we use
  1843. * special IO operations which ignore the immutable bit. */
  1844. mutex_lock_nested(&inode->i_mutex, I_MUTEX_QUOTA);
  1845. oldflags = inode->i_flags & (S_NOATIME | S_IMMUTABLE |
  1846. S_NOQUOTA);
  1847. inode->i_flags |= S_NOQUOTA | S_NOATIME | S_IMMUTABLE;
  1848. mutex_unlock(&inode->i_mutex);
  1849. /*
  1850. * When S_NOQUOTA is set, remove dquot references as no more
  1851. * references can be added
  1852. */
  1853. __dquot_drop(inode);
  1854. }
  1855. error = -EIO;
  1856. dqopt->files[type] = igrab(inode);
  1857. if (!dqopt->files[type])
  1858. goto out_lock;
  1859. error = -EINVAL;
  1860. if (!fmt->qf_ops->check_quota_file(sb, type))
  1861. goto out_file_init;
  1862. dqopt->ops[type] = fmt->qf_ops;
  1863. dqopt->info[type].dqi_format = fmt;
  1864. dqopt->info[type].dqi_fmt_id = format_id;
  1865. INIT_LIST_HEAD(&dqopt->info[type].dqi_dirty_list);
  1866. mutex_lock(&dqopt->dqio_mutex);
  1867. error = dqopt->ops[type]->read_file_info(sb, type);
  1868. if (error < 0) {
  1869. mutex_unlock(&dqopt->dqio_mutex);
  1870. goto out_file_init;
  1871. }
  1872. mutex_unlock(&dqopt->dqio_mutex);
  1873. spin_lock(&dq_state_lock);
  1874. dqopt->flags |= dquot_state_flag(flags, type);
  1875. spin_unlock(&dq_state_lock);
  1876. add_dquot_ref(sb, type);
  1877. mutex_unlock(&dqopt->dqonoff_mutex);
  1878. return 0;
  1879. out_file_init:
  1880. dqopt->files[type] = NULL;
  1881. iput(inode);
  1882. out_lock:
  1883. if (oldflags != -1) {
  1884. mutex_lock_nested(&inode->i_mutex, I_MUTEX_QUOTA);
  1885. /* Set the flags back (in the case of accidental quotaon()
  1886. * on a wrong file we don't want to mess up the flags) */
  1887. inode->i_flags &= ~(S_NOATIME | S_NOQUOTA | S_IMMUTABLE);
  1888. inode->i_flags |= oldflags;
  1889. mutex_unlock(&inode->i_mutex);
  1890. }
  1891. mutex_unlock(&dqopt->dqonoff_mutex);
  1892. out_fmt:
  1893. put_quota_format(fmt);
  1894. return error;
  1895. }
  1896. /* Reenable quotas on remount RW */
  1897. static int vfs_quota_on_remount(struct super_block *sb, int type)
  1898. {
  1899. struct quota_info *dqopt = sb_dqopt(sb);
  1900. struct inode *inode;
  1901. int ret;
  1902. unsigned int flags;
  1903. mutex_lock(&dqopt->dqonoff_mutex);
  1904. if (!sb_has_quota_suspended(sb, type)) {
  1905. mutex_unlock(&dqopt->dqonoff_mutex);
  1906. return 0;
  1907. }
  1908. inode = dqopt->files[type];
  1909. dqopt->files[type] = NULL;
  1910. spin_lock(&dq_state_lock);
  1911. flags = dqopt->flags & dquot_state_flag(DQUOT_USAGE_ENABLED |
  1912. DQUOT_LIMITS_ENABLED, type);
  1913. dqopt->flags &= ~dquot_state_flag(DQUOT_STATE_FLAGS, type);
  1914. spin_unlock(&dq_state_lock);
  1915. mutex_unlock(&dqopt->dqonoff_mutex);
  1916. flags = dquot_generic_flag(flags, type);
  1917. ret = vfs_load_quota_inode(inode, type, dqopt->info[type].dqi_fmt_id,
  1918. flags);
  1919. iput(inode);
  1920. return ret;
  1921. }
  1922. int vfs_quota_on_path(struct super_block *sb, int type, int format_id,
  1923. struct path *path)
  1924. {
  1925. int error = security_quota_on(path->dentry);
  1926. if (error)
  1927. return error;
  1928. /* Quota file not on the same filesystem? */
  1929. if (path->mnt->mnt_sb != sb)
  1930. error = -EXDEV;
  1931. else
  1932. error = vfs_load_quota_inode(path->dentry->d_inode, type,
  1933. format_id, DQUOT_USAGE_ENABLED |
  1934. DQUOT_LIMITS_ENABLED);
  1935. return error;
  1936. }
  1937. EXPORT_SYMBOL(vfs_quota_on_path);
  1938. int vfs_quota_on(struct super_block *sb, int type, int format_id, char *name,
  1939. int remount)
  1940. {
  1941. struct path path;
  1942. int error;
  1943. if (remount)
  1944. return vfs_quota_on_remount(sb, type);
  1945. error = kern_path(name, LOOKUP_FOLLOW, &path);
  1946. if (!error) {
  1947. error = vfs_quota_on_path(sb, type, format_id, &path);
  1948. path_put(&path);
  1949. }
  1950. return error;
  1951. }
  1952. EXPORT_SYMBOL(vfs_quota_on);
  1953. /*
  1954. * More powerful function for turning on quotas allowing setting
  1955. * of individual quota flags
  1956. */
  1957. int vfs_quota_enable(struct inode *inode, int type, int format_id,
  1958. unsigned int flags)
  1959. {
  1960. int ret = 0;
  1961. struct super_block *sb = inode->i_sb;
  1962. struct quota_info *dqopt = sb_dqopt(sb);
  1963. /* Just unsuspend quotas? */
  1964. if (flags & DQUOT_SUSPENDED)
  1965. return vfs_quota_on_remount(sb, type);
  1966. if (!flags)
  1967. return 0;
  1968. /* Just updating flags needed? */
  1969. if (sb_has_quota_loaded(sb, type)) {
  1970. mutex_lock(&dqopt->dqonoff_mutex);
  1971. /* Now do a reliable test... */
  1972. if (!sb_has_quota_loaded(sb, type)) {
  1973. mutex_unlock(&dqopt->dqonoff_mutex);
  1974. goto load_quota;
  1975. }
  1976. if (flags & DQUOT_USAGE_ENABLED &&
  1977. sb_has_quota_usage_enabled(sb, type)) {
  1978. ret = -EBUSY;
  1979. goto out_lock;
  1980. }
  1981. if (flags & DQUOT_LIMITS_ENABLED &&
  1982. sb_has_quota_limits_enabled(sb, type)) {
  1983. ret = -EBUSY;
  1984. goto out_lock;
  1985. }
  1986. spin_lock(&dq_state_lock);
  1987. sb_dqopt(sb)->flags |= dquot_state_flag(flags, type);
  1988. spin_unlock(&dq_state_lock);
  1989. out_lock:
  1990. mutex_unlock(&dqopt->dqonoff_mutex);
  1991. return ret;
  1992. }
  1993. load_quota:
  1994. return vfs_load_quota_inode(inode, type, format_id, flags);
  1995. }
  1996. EXPORT_SYMBOL(vfs_quota_enable);
  1997. /*
  1998. * This function is used when filesystem needs to initialize quotas
  1999. * during mount time.
  2000. */
  2001. int vfs_quota_on_mount(struct super_block *sb, char *qf_name,
  2002. int format_id, int type)
  2003. {
  2004. struct dentry *dentry;
  2005. int error;
  2006. mutex_lock(&sb->s_root->d_inode->i_mutex);
  2007. dentry = lookup_one_len(qf_name, sb->s_root, strlen(qf_name));
  2008. mutex_unlock(&sb->s_root->d_inode->i_mutex);
  2009. if (IS_ERR(dentry))
  2010. return PTR_ERR(dentry);
  2011. if (!dentry->d_inode) {
  2012. error = -ENOENT;
  2013. goto out;
  2014. }
  2015. error = security_quota_on(dentry);
  2016. if (!error)
  2017. error = vfs_load_quota_inode(dentry->d_inode, type, format_id,
  2018. DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED);
  2019. out:
  2020. dput(dentry);
  2021. return error;
  2022. }
  2023. EXPORT_SYMBOL(vfs_quota_on_mount);
  2024. /* Wrapper to turn on quotas when remounting rw */
  2025. int vfs_dq_quota_on_remount(struct super_block *sb)
  2026. {
  2027. int cnt;
  2028. int ret = 0, err;
  2029. if (!sb->s_qcop || !sb->s_qcop->quota_on)
  2030. return -ENOSYS;
  2031. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  2032. err = sb->s_qcop->quota_on(sb, cnt, 0, NULL, 1);
  2033. if (err < 0 && !ret)
  2034. ret = err;
  2035. }
  2036. return ret;
  2037. }
  2038. EXPORT_SYMBOL(vfs_dq_quota_on_remount);
  2039. static inline qsize_t qbtos(qsize_t blocks)
  2040. {
  2041. return blocks << QIF_DQBLKSIZE_BITS;
  2042. }
  2043. static inline qsize_t stoqb(qsize_t space)
  2044. {
  2045. return (space + QIF_DQBLKSIZE - 1) >> QIF_DQBLKSIZE_BITS;
  2046. }
  2047. /* Generic routine for getting common part of quota structure */
  2048. static void do_get_dqblk(struct dquot *dquot, struct if_dqblk *di)
  2049. {
  2050. struct mem_dqblk *dm = &dquot->dq_dqb;
  2051. spin_lock(&dq_data_lock);
  2052. di->dqb_bhardlimit = stoqb(dm->dqb_bhardlimit);
  2053. di->dqb_bsoftlimit = stoqb(dm->dqb_bsoftlimit);
  2054. di->dqb_curspace = dm->dqb_curspace + dm->dqb_rsvspace;
  2055. di->dqb_ihardlimit = dm->dqb_ihardlimit;
  2056. di->dqb_isoftlimit = dm->dqb_isoftlimit;
  2057. di->dqb_curinodes = dm->dqb_curinodes;
  2058. di->dqb_btime = dm->dqb_btime;
  2059. di->dqb_itime = dm->dqb_itime;
  2060. di->dqb_valid = QIF_ALL;
  2061. spin_unlock(&dq_data_lock);
  2062. }
  2063. int vfs_get_dqblk(struct super_block *sb, int type, qid_t id,
  2064. struct if_dqblk *di)
  2065. {
  2066. struct dquot *dquot;
  2067. dquot = dqget(sb, id, type);
  2068. if (!dquot)
  2069. return -ESRCH;
  2070. do_get_dqblk(dquot, di);
  2071. dqput(dquot);
  2072. return 0;
  2073. }
  2074. EXPORT_SYMBOL(vfs_get_dqblk);
  2075. /* Generic routine for setting common part of quota structure */
  2076. static int do_set_dqblk(struct dquot *dquot, struct if_dqblk *di)
  2077. {
  2078. struct mem_dqblk *dm = &dquot->dq_dqb;
  2079. int check_blim = 0, check_ilim = 0;
  2080. struct mem_dqinfo *dqi = &sb_dqopt(dquot->dq_sb)->info[dquot->dq_type];
  2081. if ((di->dqb_valid & QIF_BLIMITS &&
  2082. (di->dqb_bhardlimit > dqi->dqi_maxblimit ||
  2083. di->dqb_bsoftlimit > dqi->dqi_maxblimit)) ||
  2084. (di->dqb_valid & QIF_ILIMITS &&
  2085. (di->dqb_ihardlimit > dqi->dqi_maxilimit ||
  2086. di->dqb_isoftlimit > dqi->dqi_maxilimit)))
  2087. return -ERANGE;
  2088. spin_lock(&dq_data_lock);
  2089. if (di->dqb_valid & QIF_SPACE) {
  2090. dm->dqb_curspace = di->dqb_curspace - dm->dqb_rsvspace;
  2091. check_blim = 1;
  2092. __set_bit(DQ_LASTSET_B + QIF_SPACE_B, &dquot->dq_flags);
  2093. }
  2094. if (di->dqb_valid & QIF_BLIMITS) {
  2095. dm->dqb_bsoftlimit = qbtos(di->dqb_bsoftlimit);
  2096. dm->dqb_bhardlimit = qbtos(di->dqb_bhardlimit);
  2097. check_blim = 1;
  2098. __set_bit(DQ_LASTSET_B + QIF_BLIMITS_B, &dquot->dq_flags);
  2099. }
  2100. if (di->dqb_valid & QIF_INODES) {
  2101. dm->dqb_curinodes = di->dqb_curinodes;
  2102. check_ilim = 1;
  2103. __set_bit(DQ_LASTSET_B + QIF_INODES_B, &dquot->dq_flags);
  2104. }
  2105. if (di->dqb_valid & QIF_ILIMITS) {
  2106. dm->dqb_isoftlimit = di->dqb_isoftlimit;
  2107. dm->dqb_ihardlimit = di->dqb_ihardlimit;
  2108. check_ilim = 1;
  2109. __set_bit(DQ_LASTSET_B + QIF_ILIMITS_B, &dquot->dq_flags);
  2110. }
  2111. if (di->dqb_valid & QIF_BTIME) {
  2112. dm->dqb_btime = di->dqb_btime;
  2113. check_blim = 1;
  2114. __set_bit(DQ_LASTSET_B + QIF_BTIME_B, &dquot->dq_flags);
  2115. }
  2116. if (di->dqb_valid & QIF_ITIME) {
  2117. dm->dqb_itime = di->dqb_itime;
  2118. check_ilim = 1;
  2119. __set_bit(DQ_LASTSET_B + QIF_ITIME_B, &dquot->dq_flags);
  2120. }
  2121. if (check_blim) {
  2122. if (!dm->dqb_bsoftlimit ||
  2123. dm->dqb_curspace < dm->dqb_bsoftlimit) {
  2124. dm->dqb_btime = 0;
  2125. clear_bit(DQ_BLKS_B, &dquot->dq_flags);
  2126. } else if (!(di->dqb_valid & QIF_BTIME))
  2127. /* Set grace only if user hasn't provided his own... */
  2128. dm->dqb_btime = get_seconds() + dqi->dqi_bgrace;
  2129. }
  2130. if (check_ilim) {
  2131. if (!dm->dqb_isoftlimit ||
  2132. dm->dqb_curinodes < dm->dqb_isoftlimit) {
  2133. dm->dqb_itime = 0;
  2134. clear_bit(DQ_INODES_B, &dquot->dq_flags);
  2135. } else if (!(di->dqb_valid & QIF_ITIME))
  2136. /* Set grace only if user hasn't provided his own... */
  2137. dm->dqb_itime = get_seconds() + dqi->dqi_igrace;
  2138. }
  2139. if (dm->dqb_bhardlimit || dm->dqb_bsoftlimit || dm->dqb_ihardlimit ||
  2140. dm->dqb_isoftlimit)
  2141. clear_bit(DQ_FAKE_B, &dquot->dq_flags);
  2142. else
  2143. set_bit(DQ_FAKE_B, &dquot->dq_flags);
  2144. spin_unlock(&dq_data_lock);
  2145. mark_dquot_dirty(dquot);
  2146. return 0;
  2147. }
  2148. int vfs_set_dqblk(struct super_block *sb, int type, qid_t id,
  2149. struct if_dqblk *di)
  2150. {
  2151. struct dquot *dquot;
  2152. int rc;
  2153. dquot = dqget(sb, id, type);
  2154. if (!dquot) {
  2155. rc = -ESRCH;
  2156. goto out;
  2157. }
  2158. rc = do_set_dqblk(dquot, di);
  2159. dqput(dquot);
  2160. out:
  2161. return rc;
  2162. }
  2163. EXPORT_SYMBOL(vfs_set_dqblk);
  2164. /* Generic routine for getting common part of quota file information */
  2165. int vfs_get_dqinfo(struct super_block *sb, int type, struct if_dqinfo *ii)
  2166. {
  2167. struct mem_dqinfo *mi;
  2168. mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
  2169. if (!sb_has_quota_active(sb, type)) {
  2170. mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
  2171. return -ESRCH;
  2172. }
  2173. mi = sb_dqopt(sb)->info + type;
  2174. spin_lock(&dq_data_lock);
  2175. ii->dqi_bgrace = mi->dqi_bgrace;
  2176. ii->dqi_igrace = mi->dqi_igrace;
  2177. ii->dqi_flags = mi->dqi_flags & DQF_MASK;
  2178. ii->dqi_valid = IIF_ALL;
  2179. spin_unlock(&dq_data_lock);
  2180. mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
  2181. return 0;
  2182. }
  2183. EXPORT_SYMBOL(vfs_get_dqinfo);
  2184. /* Generic routine for setting common part of quota file information */
  2185. int vfs_set_dqinfo(struct super_block *sb, int type, struct if_dqinfo *ii)
  2186. {
  2187. struct mem_dqinfo *mi;
  2188. int err = 0;
  2189. mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
  2190. if (!sb_has_quota_active(sb, type)) {
  2191. err = -ESRCH;
  2192. goto out;
  2193. }
  2194. mi = sb_dqopt(sb)->info + type;
  2195. spin_lock(&dq_data_lock);
  2196. if (ii->dqi_valid & IIF_BGRACE)
  2197. mi->dqi_bgrace = ii->dqi_bgrace;
  2198. if (ii->dqi_valid & IIF_IGRACE)
  2199. mi->dqi_igrace = ii->dqi_igrace;
  2200. if (ii->dqi_valid & IIF_FLAGS)
  2201. mi->dqi_flags = (mi->dqi_flags & ~DQF_MASK) |
  2202. (ii->dqi_flags & DQF_MASK);
  2203. spin_unlock(&dq_data_lock);
  2204. mark_info_dirty(sb, type);
  2205. /* Force write to disk */
  2206. sb->dq_op->write_info(sb, type);
  2207. out:
  2208. mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
  2209. return err;
  2210. }
  2211. EXPORT_SYMBOL(vfs_set_dqinfo);
  2212. const struct quotactl_ops vfs_quotactl_ops = {
  2213. .quota_on = vfs_quota_on,
  2214. .quota_off = vfs_quota_off,
  2215. .quota_sync = vfs_quota_sync,
  2216. .get_info = vfs_get_dqinfo,
  2217. .set_info = vfs_set_dqinfo,
  2218. .get_dqblk = vfs_get_dqblk,
  2219. .set_dqblk = vfs_set_dqblk
  2220. };
  2221. static ctl_table fs_dqstats_table[] = {
  2222. {
  2223. .procname = "lookups",
  2224. .data = &dqstats.lookups,
  2225. .maxlen = sizeof(int),
  2226. .mode = 0444,
  2227. .proc_handler = proc_dointvec,
  2228. },
  2229. {
  2230. .procname = "drops",
  2231. .data = &dqstats.drops,
  2232. .maxlen = sizeof(int),
  2233. .mode = 0444,
  2234. .proc_handler = proc_dointvec,
  2235. },
  2236. {
  2237. .procname = "reads",
  2238. .data = &dqstats.reads,
  2239. .maxlen = sizeof(int),
  2240. .mode = 0444,
  2241. .proc_handler = proc_dointvec,
  2242. },
  2243. {
  2244. .procname = "writes",
  2245. .data = &dqstats.writes,
  2246. .maxlen = sizeof(int),
  2247. .mode = 0444,
  2248. .proc_handler = proc_dointvec,
  2249. },
  2250. {
  2251. .procname = "cache_hits",
  2252. .data = &dqstats.cache_hits,
  2253. .maxlen = sizeof(int),
  2254. .mode = 0444,
  2255. .proc_handler = proc_dointvec,
  2256. },
  2257. {
  2258. .procname = "allocated_dquots",
  2259. .data = &dqstats.allocated_dquots,
  2260. .maxlen = sizeof(int),
  2261. .mode = 0444,
  2262. .proc_handler = proc_dointvec,
  2263. },
  2264. {
  2265. .procname = "free_dquots",
  2266. .data = &dqstats.free_dquots,
  2267. .maxlen = sizeof(int),
  2268. .mode = 0444,
  2269. .proc_handler = proc_dointvec,
  2270. },
  2271. {
  2272. .procname = "syncs",
  2273. .data = &dqstats.syncs,
  2274. .maxlen = sizeof(int),
  2275. .mode = 0444,
  2276. .proc_handler = proc_dointvec,
  2277. },
  2278. #ifdef CONFIG_PRINT_QUOTA_WARNING
  2279. {
  2280. .procname = "warnings",
  2281. .data = &flag_print_warnings,
  2282. .maxlen = sizeof(int),
  2283. .mode = 0644,
  2284. .proc_handler = proc_dointvec,
  2285. },
  2286. #endif
  2287. { },
  2288. };
  2289. static ctl_table fs_table[] = {
  2290. {
  2291. .procname = "quota",
  2292. .mode = 0555,
  2293. .child = fs_dqstats_table,
  2294. },
  2295. { },
  2296. };
  2297. static ctl_table sys_table[] = {
  2298. {
  2299. .procname = "fs",
  2300. .mode = 0555,
  2301. .child = fs_table,
  2302. },
  2303. { },
  2304. };
  2305. static int __init dquot_init(void)
  2306. {
  2307. int i;
  2308. unsigned long nr_hash, order;
  2309. printk(KERN_NOTICE "VFS: Disk quotas %s\n", __DQUOT_VERSION__);
  2310. register_sysctl_table(sys_table);
  2311. dquot_cachep = kmem_cache_create("dquot",
  2312. sizeof(struct dquot), sizeof(unsigned long) * 4,
  2313. (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
  2314. SLAB_MEM_SPREAD|SLAB_PANIC),
  2315. NULL);
  2316. order = 0;
  2317. dquot_hash = (struct hlist_head *)__get_free_pages(GFP_ATOMIC, order);
  2318. if (!dquot_hash)
  2319. panic("Cannot create dquot hash table");
  2320. /* Find power-of-two hlist_heads which can fit into allocation */
  2321. nr_hash = (1UL << order) * PAGE_SIZE / sizeof(struct hlist_head);
  2322. dq_hash_bits = 0;
  2323. do {
  2324. dq_hash_bits++;
  2325. } while (nr_hash >> dq_hash_bits);
  2326. dq_hash_bits--;
  2327. nr_hash = 1UL << dq_hash_bits;
  2328. dq_hash_mask = nr_hash - 1;
  2329. for (i = 0; i < nr_hash; i++)
  2330. INIT_HLIST_HEAD(dquot_hash + i);
  2331. printk("Dquot-cache hash table entries: %ld (order %ld, %ld bytes)\n",
  2332. nr_hash, order, (PAGE_SIZE << order));
  2333. register_shrinker(&dqcache_shrinker);
  2334. return 0;
  2335. }
  2336. module_init(dquot_init);