dquot.c 73 KB

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