dquot.c 79 KB

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