dquot.c 72 KB

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