dquot.c 74 KB

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