dquot.c 72 KB

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