dquot.c 77 KB

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