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. got[cnt] = NULL;
  1269. if (type != -1 && cnt != type)
  1270. continue;
  1271. /*
  1272. * The i_dquot should have been initialized in most cases,
  1273. * we check it without locking here to avoid unnecessary
  1274. * dqget()/dqput() calls.
  1275. */
  1276. if (dquots[cnt])
  1277. continue;
  1278. if (!sb_has_quota_active(sb, cnt))
  1279. continue;
  1280. init_needed = 1;
  1281. switch (cnt) {
  1282. case USRQUOTA:
  1283. qid = make_kqid_uid(inode->i_uid);
  1284. break;
  1285. case GRPQUOTA:
  1286. qid = make_kqid_gid(inode->i_gid);
  1287. break;
  1288. case PRJQUOTA:
  1289. rc = inode->i_sb->dq_op->get_projid(inode, &projid);
  1290. if (rc)
  1291. continue;
  1292. qid = make_kqid_projid(projid);
  1293. break;
  1294. }
  1295. dquot = dqget(sb, qid);
  1296. if (IS_ERR(dquot)) {
  1297. /* We raced with somebody turning quotas off... */
  1298. if (PTR_ERR(dquot) != -ESRCH) {
  1299. ret = PTR_ERR(dquot);
  1300. goto out_put;
  1301. }
  1302. dquot = NULL;
  1303. }
  1304. got[cnt] = dquot;
  1305. }
  1306. /* All required i_dquot has been initialized */
  1307. if (!init_needed)
  1308. return 0;
  1309. spin_lock(&dq_data_lock);
  1310. if (IS_NOQUOTA(inode))
  1311. goto out_lock;
  1312. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1313. if (type != -1 && cnt != type)
  1314. continue;
  1315. /* Avoid races with quotaoff() */
  1316. if (!sb_has_quota_active(sb, cnt))
  1317. continue;
  1318. /* We could race with quotaon or dqget() could have failed */
  1319. if (!got[cnt])
  1320. continue;
  1321. if (!dquots[cnt]) {
  1322. dquots[cnt] = got[cnt];
  1323. got[cnt] = NULL;
  1324. /*
  1325. * Make quota reservation system happy if someone
  1326. * did a write before quota was turned on
  1327. */
  1328. rsv = inode_get_rsv_space(inode);
  1329. if (unlikely(rsv))
  1330. dquot_resv_space(dquots[cnt], rsv);
  1331. }
  1332. }
  1333. out_lock:
  1334. spin_unlock(&dq_data_lock);
  1335. out_put:
  1336. /* Drop unused references */
  1337. dqput_all(got);
  1338. return ret;
  1339. }
  1340. int dquot_initialize(struct inode *inode)
  1341. {
  1342. return __dquot_initialize(inode, -1);
  1343. }
  1344. EXPORT_SYMBOL(dquot_initialize);
  1345. /*
  1346. * Release all quotas referenced by inode.
  1347. *
  1348. * This function only be called on inode free or converting
  1349. * a file to quota file, no other users for the i_dquot in
  1350. * both cases, so we needn't call synchronize_srcu() after
  1351. * clearing i_dquot.
  1352. */
  1353. static void __dquot_drop(struct inode *inode)
  1354. {
  1355. int cnt;
  1356. struct dquot **dquots = i_dquot(inode);
  1357. struct dquot *put[MAXQUOTAS];
  1358. spin_lock(&dq_data_lock);
  1359. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1360. put[cnt] = dquots[cnt];
  1361. dquots[cnt] = NULL;
  1362. }
  1363. spin_unlock(&dq_data_lock);
  1364. dqput_all(put);
  1365. }
  1366. void dquot_drop(struct inode *inode)
  1367. {
  1368. struct dquot * const *dquots;
  1369. int cnt;
  1370. if (IS_NOQUOTA(inode))
  1371. return;
  1372. /*
  1373. * Test before calling to rule out calls from proc and such
  1374. * where we are not allowed to block. Note that this is
  1375. * actually reliable test even without the lock - the caller
  1376. * must assure that nobody can come after the DQUOT_DROP and
  1377. * add quota pointers back anyway.
  1378. */
  1379. dquots = i_dquot(inode);
  1380. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1381. if (dquots[cnt])
  1382. break;
  1383. }
  1384. if (cnt < MAXQUOTAS)
  1385. __dquot_drop(inode);
  1386. }
  1387. EXPORT_SYMBOL(dquot_drop);
  1388. /*
  1389. * inode_reserved_space is managed internally by quota, and protected by
  1390. * i_lock similar to i_blocks+i_bytes.
  1391. */
  1392. static qsize_t *inode_reserved_space(struct inode * inode)
  1393. {
  1394. /* Filesystem must explicitly define it's own method in order to use
  1395. * quota reservation interface */
  1396. BUG_ON(!inode->i_sb->dq_op->get_reserved_space);
  1397. return inode->i_sb->dq_op->get_reserved_space(inode);
  1398. }
  1399. void inode_add_rsv_space(struct inode *inode, qsize_t number)
  1400. {
  1401. spin_lock(&inode->i_lock);
  1402. *inode_reserved_space(inode) += number;
  1403. spin_unlock(&inode->i_lock);
  1404. }
  1405. EXPORT_SYMBOL(inode_add_rsv_space);
  1406. void inode_claim_rsv_space(struct inode *inode, qsize_t number)
  1407. {
  1408. spin_lock(&inode->i_lock);
  1409. *inode_reserved_space(inode) -= number;
  1410. __inode_add_bytes(inode, number);
  1411. spin_unlock(&inode->i_lock);
  1412. }
  1413. EXPORT_SYMBOL(inode_claim_rsv_space);
  1414. void inode_reclaim_rsv_space(struct inode *inode, qsize_t number)
  1415. {
  1416. spin_lock(&inode->i_lock);
  1417. *inode_reserved_space(inode) += number;
  1418. __inode_sub_bytes(inode, number);
  1419. spin_unlock(&inode->i_lock);
  1420. }
  1421. EXPORT_SYMBOL(inode_reclaim_rsv_space);
  1422. void inode_sub_rsv_space(struct inode *inode, qsize_t number)
  1423. {
  1424. spin_lock(&inode->i_lock);
  1425. *inode_reserved_space(inode) -= number;
  1426. spin_unlock(&inode->i_lock);
  1427. }
  1428. EXPORT_SYMBOL(inode_sub_rsv_space);
  1429. static qsize_t inode_get_rsv_space(struct inode *inode)
  1430. {
  1431. qsize_t ret;
  1432. if (!inode->i_sb->dq_op->get_reserved_space)
  1433. return 0;
  1434. spin_lock(&inode->i_lock);
  1435. ret = *inode_reserved_space(inode);
  1436. spin_unlock(&inode->i_lock);
  1437. return ret;
  1438. }
  1439. static void inode_incr_space(struct inode *inode, qsize_t number,
  1440. int reserve)
  1441. {
  1442. if (reserve)
  1443. inode_add_rsv_space(inode, number);
  1444. else
  1445. inode_add_bytes(inode, number);
  1446. }
  1447. static void inode_decr_space(struct inode *inode, qsize_t number, int reserve)
  1448. {
  1449. if (reserve)
  1450. inode_sub_rsv_space(inode, number);
  1451. else
  1452. inode_sub_bytes(inode, number);
  1453. }
  1454. /*
  1455. * This functions updates i_blocks+i_bytes fields and quota information
  1456. * (together with appropriate checks).
  1457. *
  1458. * NOTE: We absolutely rely on the fact that caller dirties the inode
  1459. * (usually helpers in quotaops.h care about this) and holds a handle for
  1460. * the current transaction so that dquot write and inode write go into the
  1461. * same transaction.
  1462. */
  1463. /*
  1464. * This operation can block, but only after everything is updated
  1465. */
  1466. int __dquot_alloc_space(struct inode *inode, qsize_t number, int flags)
  1467. {
  1468. int cnt, ret = 0, index;
  1469. struct dquot_warn warn[MAXQUOTAS];
  1470. int reserve = flags & DQUOT_SPACE_RESERVE;
  1471. struct dquot **dquots;
  1472. if (!dquot_active(inode)) {
  1473. inode_incr_space(inode, number, reserve);
  1474. goto out;
  1475. }
  1476. for (cnt = 0; cnt < MAXQUOTAS; cnt++)
  1477. warn[cnt].w_type = QUOTA_NL_NOWARN;
  1478. dquots = i_dquot(inode);
  1479. index = srcu_read_lock(&dquot_srcu);
  1480. spin_lock(&dq_data_lock);
  1481. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1482. if (!dquots[cnt])
  1483. continue;
  1484. ret = check_bdq(dquots[cnt], number,
  1485. !(flags & DQUOT_SPACE_WARN), &warn[cnt]);
  1486. if (ret && !(flags & DQUOT_SPACE_NOFAIL)) {
  1487. spin_unlock(&dq_data_lock);
  1488. goto out_flush_warn;
  1489. }
  1490. }
  1491. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1492. if (!dquots[cnt])
  1493. continue;
  1494. if (reserve)
  1495. dquot_resv_space(dquots[cnt], number);
  1496. else
  1497. dquot_incr_space(dquots[cnt], number);
  1498. }
  1499. inode_incr_space(inode, number, reserve);
  1500. spin_unlock(&dq_data_lock);
  1501. if (reserve)
  1502. goto out_flush_warn;
  1503. mark_all_dquot_dirty(dquots);
  1504. out_flush_warn:
  1505. srcu_read_unlock(&dquot_srcu, index);
  1506. flush_warnings(warn);
  1507. out:
  1508. return ret;
  1509. }
  1510. EXPORT_SYMBOL(__dquot_alloc_space);
  1511. /*
  1512. * This operation can block, but only after everything is updated
  1513. */
  1514. int dquot_alloc_inode(struct inode *inode)
  1515. {
  1516. int cnt, ret = 0, index;
  1517. struct dquot_warn warn[MAXQUOTAS];
  1518. struct dquot * const *dquots;
  1519. if (!dquot_active(inode))
  1520. return 0;
  1521. for (cnt = 0; cnt < MAXQUOTAS; cnt++)
  1522. warn[cnt].w_type = QUOTA_NL_NOWARN;
  1523. dquots = i_dquot(inode);
  1524. index = srcu_read_lock(&dquot_srcu);
  1525. spin_lock(&dq_data_lock);
  1526. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1527. if (!dquots[cnt])
  1528. continue;
  1529. ret = check_idq(dquots[cnt], 1, &warn[cnt]);
  1530. if (ret)
  1531. goto warn_put_all;
  1532. }
  1533. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1534. if (!dquots[cnt])
  1535. continue;
  1536. dquot_incr_inodes(dquots[cnt], 1);
  1537. }
  1538. warn_put_all:
  1539. spin_unlock(&dq_data_lock);
  1540. if (ret == 0)
  1541. mark_all_dquot_dirty(dquots);
  1542. srcu_read_unlock(&dquot_srcu, index);
  1543. flush_warnings(warn);
  1544. return ret;
  1545. }
  1546. EXPORT_SYMBOL(dquot_alloc_inode);
  1547. /*
  1548. * Convert in-memory reserved quotas to real consumed quotas
  1549. */
  1550. int dquot_claim_space_nodirty(struct inode *inode, qsize_t number)
  1551. {
  1552. struct dquot **dquots;
  1553. int cnt, index;
  1554. if (!dquot_active(inode)) {
  1555. inode_claim_rsv_space(inode, number);
  1556. return 0;
  1557. }
  1558. dquots = i_dquot(inode);
  1559. index = srcu_read_lock(&dquot_srcu);
  1560. spin_lock(&dq_data_lock);
  1561. /* Claim reserved quotas to allocated quotas */
  1562. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1563. if (dquots[cnt])
  1564. dquot_claim_reserved_space(dquots[cnt], number);
  1565. }
  1566. /* Update inode bytes */
  1567. inode_claim_rsv_space(inode, number);
  1568. spin_unlock(&dq_data_lock);
  1569. mark_all_dquot_dirty(dquots);
  1570. srcu_read_unlock(&dquot_srcu, index);
  1571. return 0;
  1572. }
  1573. EXPORT_SYMBOL(dquot_claim_space_nodirty);
  1574. /*
  1575. * Convert allocated space back to in-memory reserved quotas
  1576. */
  1577. void dquot_reclaim_space_nodirty(struct inode *inode, qsize_t number)
  1578. {
  1579. struct dquot **dquots;
  1580. int cnt, index;
  1581. if (!dquot_active(inode)) {
  1582. inode_reclaim_rsv_space(inode, number);
  1583. return;
  1584. }
  1585. dquots = i_dquot(inode);
  1586. index = srcu_read_lock(&dquot_srcu);
  1587. spin_lock(&dq_data_lock);
  1588. /* Claim reserved quotas to allocated quotas */
  1589. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1590. if (dquots[cnt])
  1591. dquot_reclaim_reserved_space(dquots[cnt], number);
  1592. }
  1593. /* Update inode bytes */
  1594. inode_reclaim_rsv_space(inode, number);
  1595. spin_unlock(&dq_data_lock);
  1596. mark_all_dquot_dirty(dquots);
  1597. srcu_read_unlock(&dquot_srcu, index);
  1598. return;
  1599. }
  1600. EXPORT_SYMBOL(dquot_reclaim_space_nodirty);
  1601. /*
  1602. * This operation can block, but only after everything is updated
  1603. */
  1604. void __dquot_free_space(struct inode *inode, qsize_t number, int flags)
  1605. {
  1606. unsigned int cnt;
  1607. struct dquot_warn warn[MAXQUOTAS];
  1608. struct dquot **dquots;
  1609. int reserve = flags & DQUOT_SPACE_RESERVE, index;
  1610. if (!dquot_active(inode)) {
  1611. inode_decr_space(inode, number, reserve);
  1612. return;
  1613. }
  1614. dquots = i_dquot(inode);
  1615. index = srcu_read_lock(&dquot_srcu);
  1616. spin_lock(&dq_data_lock);
  1617. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1618. int wtype;
  1619. warn[cnt].w_type = QUOTA_NL_NOWARN;
  1620. if (!dquots[cnt])
  1621. continue;
  1622. wtype = info_bdq_free(dquots[cnt], number);
  1623. if (wtype != QUOTA_NL_NOWARN)
  1624. prepare_warning(&warn[cnt], dquots[cnt], wtype);
  1625. if (reserve)
  1626. dquot_free_reserved_space(dquots[cnt], number);
  1627. else
  1628. dquot_decr_space(dquots[cnt], number);
  1629. }
  1630. inode_decr_space(inode, number, reserve);
  1631. spin_unlock(&dq_data_lock);
  1632. if (reserve)
  1633. goto out_unlock;
  1634. mark_all_dquot_dirty(dquots);
  1635. out_unlock:
  1636. srcu_read_unlock(&dquot_srcu, index);
  1637. flush_warnings(warn);
  1638. }
  1639. EXPORT_SYMBOL(__dquot_free_space);
  1640. /*
  1641. * This operation can block, but only after everything is updated
  1642. */
  1643. void dquot_free_inode(struct inode *inode)
  1644. {
  1645. unsigned int cnt;
  1646. struct dquot_warn warn[MAXQUOTAS];
  1647. struct dquot * const *dquots;
  1648. int index;
  1649. if (!dquot_active(inode))
  1650. return;
  1651. dquots = i_dquot(inode);
  1652. index = srcu_read_lock(&dquot_srcu);
  1653. spin_lock(&dq_data_lock);
  1654. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1655. int wtype;
  1656. warn[cnt].w_type = QUOTA_NL_NOWARN;
  1657. if (!dquots[cnt])
  1658. continue;
  1659. wtype = info_idq_free(dquots[cnt], 1);
  1660. if (wtype != QUOTA_NL_NOWARN)
  1661. prepare_warning(&warn[cnt], dquots[cnt], wtype);
  1662. dquot_decr_inodes(dquots[cnt], 1);
  1663. }
  1664. spin_unlock(&dq_data_lock);
  1665. mark_all_dquot_dirty(dquots);
  1666. srcu_read_unlock(&dquot_srcu, index);
  1667. flush_warnings(warn);
  1668. }
  1669. EXPORT_SYMBOL(dquot_free_inode);
  1670. /*
  1671. * Transfer the number of inode and blocks from one diskquota to an other.
  1672. * On success, dquot references in transfer_to are consumed and references
  1673. * to original dquots that need to be released are placed there. On failure,
  1674. * references are kept untouched.
  1675. *
  1676. * This operation can block, but only after everything is updated
  1677. * A transaction must be started when entering this function.
  1678. *
  1679. * We are holding reference on transfer_from & transfer_to, no need to
  1680. * protect them by srcu_read_lock().
  1681. */
  1682. int __dquot_transfer(struct inode *inode, struct dquot **transfer_to)
  1683. {
  1684. qsize_t space, cur_space;
  1685. qsize_t rsv_space = 0;
  1686. struct dquot *transfer_from[MAXQUOTAS] = {};
  1687. int cnt, ret = 0;
  1688. char is_valid[MAXQUOTAS] = {};
  1689. struct dquot_warn warn_to[MAXQUOTAS];
  1690. struct dquot_warn warn_from_inodes[MAXQUOTAS];
  1691. struct dquot_warn warn_from_space[MAXQUOTAS];
  1692. if (IS_NOQUOTA(inode))
  1693. return 0;
  1694. /* Initialize the arrays */
  1695. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1696. warn_to[cnt].w_type = QUOTA_NL_NOWARN;
  1697. warn_from_inodes[cnt].w_type = QUOTA_NL_NOWARN;
  1698. warn_from_space[cnt].w_type = QUOTA_NL_NOWARN;
  1699. }
  1700. spin_lock(&dq_data_lock);
  1701. if (IS_NOQUOTA(inode)) { /* File without quota accounting? */
  1702. spin_unlock(&dq_data_lock);
  1703. return 0;
  1704. }
  1705. cur_space = inode_get_bytes(inode);
  1706. rsv_space = inode_get_rsv_space(inode);
  1707. space = cur_space + rsv_space;
  1708. /* Build the transfer_from list and check the limits */
  1709. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1710. /*
  1711. * Skip changes for same uid or gid or for turned off quota-type.
  1712. */
  1713. if (!transfer_to[cnt])
  1714. continue;
  1715. /* Avoid races with quotaoff() */
  1716. if (!sb_has_quota_active(inode->i_sb, cnt))
  1717. continue;
  1718. is_valid[cnt] = 1;
  1719. transfer_from[cnt] = i_dquot(inode)[cnt];
  1720. ret = check_idq(transfer_to[cnt], 1, &warn_to[cnt]);
  1721. if (ret)
  1722. goto over_quota;
  1723. ret = check_bdq(transfer_to[cnt], space, 0, &warn_to[cnt]);
  1724. if (ret)
  1725. goto over_quota;
  1726. }
  1727. /*
  1728. * Finally perform the needed transfer from transfer_from to transfer_to
  1729. */
  1730. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1731. if (!is_valid[cnt])
  1732. continue;
  1733. /* Due to IO error we might not have transfer_from[] structure */
  1734. if (transfer_from[cnt]) {
  1735. int wtype;
  1736. wtype = info_idq_free(transfer_from[cnt], 1);
  1737. if (wtype != QUOTA_NL_NOWARN)
  1738. prepare_warning(&warn_from_inodes[cnt],
  1739. transfer_from[cnt], wtype);
  1740. wtype = info_bdq_free(transfer_from[cnt], space);
  1741. if (wtype != QUOTA_NL_NOWARN)
  1742. prepare_warning(&warn_from_space[cnt],
  1743. transfer_from[cnt], wtype);
  1744. dquot_decr_inodes(transfer_from[cnt], 1);
  1745. dquot_decr_space(transfer_from[cnt], cur_space);
  1746. dquot_free_reserved_space(transfer_from[cnt],
  1747. rsv_space);
  1748. }
  1749. dquot_incr_inodes(transfer_to[cnt], 1);
  1750. dquot_incr_space(transfer_to[cnt], cur_space);
  1751. dquot_resv_space(transfer_to[cnt], rsv_space);
  1752. i_dquot(inode)[cnt] = transfer_to[cnt];
  1753. }
  1754. spin_unlock(&dq_data_lock);
  1755. mark_all_dquot_dirty(transfer_from);
  1756. mark_all_dquot_dirty(transfer_to);
  1757. flush_warnings(warn_to);
  1758. flush_warnings(warn_from_inodes);
  1759. flush_warnings(warn_from_space);
  1760. /* Pass back references to put */
  1761. for (cnt = 0; cnt < MAXQUOTAS; cnt++)
  1762. if (is_valid[cnt])
  1763. transfer_to[cnt] = transfer_from[cnt];
  1764. return 0;
  1765. over_quota:
  1766. spin_unlock(&dq_data_lock);
  1767. flush_warnings(warn_to);
  1768. return ret;
  1769. }
  1770. EXPORT_SYMBOL(__dquot_transfer);
  1771. /* Wrapper for transferring ownership of an inode for uid/gid only
  1772. * Called from FSXXX_setattr()
  1773. */
  1774. int dquot_transfer(struct inode *inode, struct iattr *iattr)
  1775. {
  1776. struct dquot *transfer_to[MAXQUOTAS] = {};
  1777. struct dquot *dquot;
  1778. struct super_block *sb = inode->i_sb;
  1779. int ret;
  1780. if (!dquot_active(inode))
  1781. return 0;
  1782. if (iattr->ia_valid & ATTR_UID && !uid_eq(iattr->ia_uid, inode->i_uid)){
  1783. dquot = dqget(sb, make_kqid_uid(iattr->ia_uid));
  1784. if (IS_ERR(dquot)) {
  1785. if (PTR_ERR(dquot) != -ESRCH) {
  1786. ret = PTR_ERR(dquot);
  1787. goto out_put;
  1788. }
  1789. dquot = NULL;
  1790. }
  1791. transfer_to[USRQUOTA] = dquot;
  1792. }
  1793. if (iattr->ia_valid & ATTR_GID && !gid_eq(iattr->ia_gid, inode->i_gid)){
  1794. dquot = dqget(sb, make_kqid_gid(iattr->ia_gid));
  1795. if (IS_ERR(dquot)) {
  1796. if (PTR_ERR(dquot) != -ESRCH) {
  1797. ret = PTR_ERR(dquot);
  1798. goto out_put;
  1799. }
  1800. dquot = NULL;
  1801. }
  1802. transfer_to[GRPQUOTA] = dquot;
  1803. }
  1804. ret = __dquot_transfer(inode, transfer_to);
  1805. out_put:
  1806. dqput_all(transfer_to);
  1807. return ret;
  1808. }
  1809. EXPORT_SYMBOL(dquot_transfer);
  1810. /*
  1811. * Write info of quota file to disk
  1812. */
  1813. int dquot_commit_info(struct super_block *sb, int type)
  1814. {
  1815. int ret;
  1816. struct quota_info *dqopt = sb_dqopt(sb);
  1817. mutex_lock(&dqopt->dqio_mutex);
  1818. ret = dqopt->ops[type]->write_file_info(sb, type);
  1819. mutex_unlock(&dqopt->dqio_mutex);
  1820. return ret;
  1821. }
  1822. EXPORT_SYMBOL(dquot_commit_info);
  1823. /*
  1824. * Definitions of diskquota operations.
  1825. */
  1826. const struct dquot_operations dquot_operations = {
  1827. .write_dquot = dquot_commit,
  1828. .acquire_dquot = dquot_acquire,
  1829. .release_dquot = dquot_release,
  1830. .mark_dirty = dquot_mark_dquot_dirty,
  1831. .write_info = dquot_commit_info,
  1832. .alloc_dquot = dquot_alloc,
  1833. .destroy_dquot = dquot_destroy,
  1834. };
  1835. EXPORT_SYMBOL(dquot_operations);
  1836. /*
  1837. * Generic helper for ->open on filesystems supporting disk quotas.
  1838. */
  1839. int dquot_file_open(struct inode *inode, struct file *file)
  1840. {
  1841. int error;
  1842. error = generic_file_open(inode, file);
  1843. if (!error && (file->f_mode & FMODE_WRITE))
  1844. dquot_initialize(inode);
  1845. return error;
  1846. }
  1847. EXPORT_SYMBOL(dquot_file_open);
  1848. /*
  1849. * Turn quota off on a device. type == -1 ==> quotaoff for all types (umount)
  1850. */
  1851. int dquot_disable(struct super_block *sb, int type, unsigned int flags)
  1852. {
  1853. int cnt, ret = 0;
  1854. struct quota_info *dqopt = sb_dqopt(sb);
  1855. struct inode *toputinode[MAXQUOTAS];
  1856. /* Cannot turn off usage accounting without turning off limits, or
  1857. * suspend quotas and simultaneously turn quotas off. */
  1858. if ((flags & DQUOT_USAGE_ENABLED && !(flags & DQUOT_LIMITS_ENABLED))
  1859. || (flags & DQUOT_SUSPENDED && flags & (DQUOT_LIMITS_ENABLED |
  1860. DQUOT_USAGE_ENABLED)))
  1861. return -EINVAL;
  1862. /* We need to serialize quota_off() for device */
  1863. mutex_lock(&dqopt->dqonoff_mutex);
  1864. /*
  1865. * Skip everything if there's nothing to do. We have to do this because
  1866. * sometimes we are called when fill_super() failed and calling
  1867. * sync_fs() in such cases does no good.
  1868. */
  1869. if (!sb_any_quota_loaded(sb)) {
  1870. mutex_unlock(&dqopt->dqonoff_mutex);
  1871. return 0;
  1872. }
  1873. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1874. toputinode[cnt] = NULL;
  1875. if (type != -1 && cnt != type)
  1876. continue;
  1877. if (!sb_has_quota_loaded(sb, cnt))
  1878. continue;
  1879. if (flags & DQUOT_SUSPENDED) {
  1880. spin_lock(&dq_state_lock);
  1881. dqopt->flags |=
  1882. dquot_state_flag(DQUOT_SUSPENDED, cnt);
  1883. spin_unlock(&dq_state_lock);
  1884. } else {
  1885. spin_lock(&dq_state_lock);
  1886. dqopt->flags &= ~dquot_state_flag(flags, cnt);
  1887. /* Turning off suspended quotas? */
  1888. if (!sb_has_quota_loaded(sb, cnt) &&
  1889. sb_has_quota_suspended(sb, cnt)) {
  1890. dqopt->flags &= ~dquot_state_flag(
  1891. DQUOT_SUSPENDED, cnt);
  1892. spin_unlock(&dq_state_lock);
  1893. iput(dqopt->files[cnt]);
  1894. dqopt->files[cnt] = NULL;
  1895. continue;
  1896. }
  1897. spin_unlock(&dq_state_lock);
  1898. }
  1899. /* We still have to keep quota loaded? */
  1900. if (sb_has_quota_loaded(sb, cnt) && !(flags & DQUOT_SUSPENDED))
  1901. continue;
  1902. /* Note: these are blocking operations */
  1903. drop_dquot_ref(sb, cnt);
  1904. invalidate_dquots(sb, cnt);
  1905. /*
  1906. * Now all dquots should be invalidated, all writes done so we
  1907. * should be only users of the info. No locks needed.
  1908. */
  1909. if (info_dirty(&dqopt->info[cnt]))
  1910. sb->dq_op->write_info(sb, cnt);
  1911. if (dqopt->ops[cnt]->free_file_info)
  1912. dqopt->ops[cnt]->free_file_info(sb, cnt);
  1913. put_quota_format(dqopt->info[cnt].dqi_format);
  1914. toputinode[cnt] = dqopt->files[cnt];
  1915. if (!sb_has_quota_loaded(sb, cnt))
  1916. dqopt->files[cnt] = NULL;
  1917. dqopt->info[cnt].dqi_flags = 0;
  1918. dqopt->info[cnt].dqi_igrace = 0;
  1919. dqopt->info[cnt].dqi_bgrace = 0;
  1920. dqopt->ops[cnt] = NULL;
  1921. }
  1922. mutex_unlock(&dqopt->dqonoff_mutex);
  1923. /* Skip syncing and setting flags if quota files are hidden */
  1924. if (dqopt->flags & DQUOT_QUOTA_SYS_FILE)
  1925. goto put_inodes;
  1926. /* Sync the superblock so that buffers with quota data are written to
  1927. * disk (and so userspace sees correct data afterwards). */
  1928. if (sb->s_op->sync_fs)
  1929. sb->s_op->sync_fs(sb, 1);
  1930. sync_blockdev(sb->s_bdev);
  1931. /* Now the quota files are just ordinary files and we can set the
  1932. * inode flags back. Moreover we discard the pagecache so that
  1933. * userspace sees the writes we did bypassing the pagecache. We
  1934. * must also discard the blockdev buffers so that we see the
  1935. * changes done by userspace on the next quotaon() */
  1936. for (cnt = 0; cnt < MAXQUOTAS; cnt++)
  1937. if (toputinode[cnt]) {
  1938. mutex_lock(&dqopt->dqonoff_mutex);
  1939. /* If quota was reenabled in the meantime, we have
  1940. * nothing to do */
  1941. if (!sb_has_quota_loaded(sb, cnt)) {
  1942. mutex_lock(&toputinode[cnt]->i_mutex);
  1943. toputinode[cnt]->i_flags &= ~(S_IMMUTABLE |
  1944. S_NOATIME | S_NOQUOTA);
  1945. truncate_inode_pages(&toputinode[cnt]->i_data,
  1946. 0);
  1947. mutex_unlock(&toputinode[cnt]->i_mutex);
  1948. mark_inode_dirty_sync(toputinode[cnt]);
  1949. }
  1950. mutex_unlock(&dqopt->dqonoff_mutex);
  1951. }
  1952. if (sb->s_bdev)
  1953. invalidate_bdev(sb->s_bdev);
  1954. put_inodes:
  1955. for (cnt = 0; cnt < MAXQUOTAS; cnt++)
  1956. if (toputinode[cnt]) {
  1957. /* On remount RO, we keep the inode pointer so that we
  1958. * can reenable quota on the subsequent remount RW. We
  1959. * have to check 'flags' variable and not use sb_has_
  1960. * function because another quotaon / quotaoff could
  1961. * change global state before we got here. We refuse
  1962. * to suspend quotas when there is pending delete on
  1963. * the quota file... */
  1964. if (!(flags & DQUOT_SUSPENDED))
  1965. iput(toputinode[cnt]);
  1966. else if (!toputinode[cnt]->i_nlink)
  1967. ret = -EBUSY;
  1968. }
  1969. return ret;
  1970. }
  1971. EXPORT_SYMBOL(dquot_disable);
  1972. int dquot_quota_off(struct super_block *sb, int type)
  1973. {
  1974. return dquot_disable(sb, type,
  1975. DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED);
  1976. }
  1977. EXPORT_SYMBOL(dquot_quota_off);
  1978. /*
  1979. * Turn quotas on on a device
  1980. */
  1981. /*
  1982. * Helper function to turn quotas on when we already have the inode of
  1983. * quota file and no quota information is loaded.
  1984. */
  1985. static int vfs_load_quota_inode(struct inode *inode, int type, int format_id,
  1986. unsigned int flags)
  1987. {
  1988. struct quota_format_type *fmt = find_quota_format(format_id);
  1989. struct super_block *sb = inode->i_sb;
  1990. struct quota_info *dqopt = sb_dqopt(sb);
  1991. int error;
  1992. int oldflags = -1;
  1993. if (!fmt)
  1994. return -ESRCH;
  1995. if (!S_ISREG(inode->i_mode)) {
  1996. error = -EACCES;
  1997. goto out_fmt;
  1998. }
  1999. if (IS_RDONLY(inode)) {
  2000. error = -EROFS;
  2001. goto out_fmt;
  2002. }
  2003. if (!sb->s_op->quota_write || !sb->s_op->quota_read ||
  2004. (type == PRJQUOTA && sb->dq_op->get_projid == NULL)) {
  2005. error = -EINVAL;
  2006. goto out_fmt;
  2007. }
  2008. /* Usage always has to be set... */
  2009. if (!(flags & DQUOT_USAGE_ENABLED)) {
  2010. error = -EINVAL;
  2011. goto out_fmt;
  2012. }
  2013. if (!(dqopt->flags & DQUOT_QUOTA_SYS_FILE)) {
  2014. /* As we bypass the pagecache we must now flush all the
  2015. * dirty data and invalidate caches so that kernel sees
  2016. * changes from userspace. It is not enough to just flush
  2017. * the quota file since if blocksize < pagesize, invalidation
  2018. * of the cache could fail because of other unrelated dirty
  2019. * data */
  2020. sync_filesystem(sb);
  2021. invalidate_bdev(sb->s_bdev);
  2022. }
  2023. mutex_lock(&dqopt->dqonoff_mutex);
  2024. if (sb_has_quota_loaded(sb, type)) {
  2025. error = -EBUSY;
  2026. goto out_lock;
  2027. }
  2028. if (!(dqopt->flags & DQUOT_QUOTA_SYS_FILE)) {
  2029. /* We don't want quota and atime on quota files (deadlocks
  2030. * possible) Also nobody should write to the file - we use
  2031. * special IO operations which ignore the immutable bit. */
  2032. mutex_lock(&inode->i_mutex);
  2033. oldflags = inode->i_flags & (S_NOATIME | S_IMMUTABLE |
  2034. S_NOQUOTA);
  2035. inode->i_flags |= S_NOQUOTA | S_NOATIME | S_IMMUTABLE;
  2036. mutex_unlock(&inode->i_mutex);
  2037. /*
  2038. * When S_NOQUOTA is set, remove dquot references as no more
  2039. * references can be added
  2040. */
  2041. __dquot_drop(inode);
  2042. }
  2043. error = -EIO;
  2044. dqopt->files[type] = igrab(inode);
  2045. if (!dqopt->files[type])
  2046. goto out_lock;
  2047. error = -EINVAL;
  2048. if (!fmt->qf_ops->check_quota_file(sb, type))
  2049. goto out_file_init;
  2050. dqopt->ops[type] = fmt->qf_ops;
  2051. dqopt->info[type].dqi_format = fmt;
  2052. dqopt->info[type].dqi_fmt_id = format_id;
  2053. INIT_LIST_HEAD(&dqopt->info[type].dqi_dirty_list);
  2054. mutex_lock(&dqopt->dqio_mutex);
  2055. error = dqopt->ops[type]->read_file_info(sb, type);
  2056. if (error < 0) {
  2057. mutex_unlock(&dqopt->dqio_mutex);
  2058. goto out_file_init;
  2059. }
  2060. if (dqopt->flags & DQUOT_QUOTA_SYS_FILE)
  2061. dqopt->info[type].dqi_flags |= DQF_SYS_FILE;
  2062. mutex_unlock(&dqopt->dqio_mutex);
  2063. spin_lock(&dq_state_lock);
  2064. dqopt->flags |= dquot_state_flag(flags, type);
  2065. spin_unlock(&dq_state_lock);
  2066. add_dquot_ref(sb, type);
  2067. mutex_unlock(&dqopt->dqonoff_mutex);
  2068. return 0;
  2069. out_file_init:
  2070. dqopt->files[type] = NULL;
  2071. iput(inode);
  2072. out_lock:
  2073. if (oldflags != -1) {
  2074. mutex_lock(&inode->i_mutex);
  2075. /* Set the flags back (in the case of accidental quotaon()
  2076. * on a wrong file we don't want to mess up the flags) */
  2077. inode->i_flags &= ~(S_NOATIME | S_NOQUOTA | S_IMMUTABLE);
  2078. inode->i_flags |= oldflags;
  2079. mutex_unlock(&inode->i_mutex);
  2080. }
  2081. mutex_unlock(&dqopt->dqonoff_mutex);
  2082. out_fmt:
  2083. put_quota_format(fmt);
  2084. return error;
  2085. }
  2086. /* Reenable quotas on remount RW */
  2087. int dquot_resume(struct super_block *sb, int type)
  2088. {
  2089. struct quota_info *dqopt = sb_dqopt(sb);
  2090. struct inode *inode;
  2091. int ret = 0, cnt;
  2092. unsigned int flags;
  2093. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  2094. if (type != -1 && cnt != type)
  2095. continue;
  2096. mutex_lock(&dqopt->dqonoff_mutex);
  2097. if (!sb_has_quota_suspended(sb, cnt)) {
  2098. mutex_unlock(&dqopt->dqonoff_mutex);
  2099. continue;
  2100. }
  2101. inode = dqopt->files[cnt];
  2102. dqopt->files[cnt] = NULL;
  2103. spin_lock(&dq_state_lock);
  2104. flags = dqopt->flags & dquot_state_flag(DQUOT_USAGE_ENABLED |
  2105. DQUOT_LIMITS_ENABLED,
  2106. cnt);
  2107. dqopt->flags &= ~dquot_state_flag(DQUOT_STATE_FLAGS, cnt);
  2108. spin_unlock(&dq_state_lock);
  2109. mutex_unlock(&dqopt->dqonoff_mutex);
  2110. flags = dquot_generic_flag(flags, cnt);
  2111. ret = vfs_load_quota_inode(inode, cnt,
  2112. dqopt->info[cnt].dqi_fmt_id, flags);
  2113. iput(inode);
  2114. }
  2115. return ret;
  2116. }
  2117. EXPORT_SYMBOL(dquot_resume);
  2118. int dquot_quota_on(struct super_block *sb, int type, int format_id,
  2119. struct path *path)
  2120. {
  2121. int error = security_quota_on(path->dentry);
  2122. if (error)
  2123. return error;
  2124. /* Quota file not on the same filesystem? */
  2125. if (path->dentry->d_sb != sb)
  2126. error = -EXDEV;
  2127. else
  2128. error = vfs_load_quota_inode(d_inode(path->dentry), type,
  2129. format_id, DQUOT_USAGE_ENABLED |
  2130. DQUOT_LIMITS_ENABLED);
  2131. return error;
  2132. }
  2133. EXPORT_SYMBOL(dquot_quota_on);
  2134. /*
  2135. * More powerful function for turning on quotas allowing setting
  2136. * of individual quota flags
  2137. */
  2138. int dquot_enable(struct inode *inode, int type, int format_id,
  2139. unsigned int flags)
  2140. {
  2141. int ret = 0;
  2142. struct super_block *sb = inode->i_sb;
  2143. struct quota_info *dqopt = sb_dqopt(sb);
  2144. /* Just unsuspend quotas? */
  2145. BUG_ON(flags & DQUOT_SUSPENDED);
  2146. if (!flags)
  2147. return 0;
  2148. /* Just updating flags needed? */
  2149. if (sb_has_quota_loaded(sb, type)) {
  2150. mutex_lock(&dqopt->dqonoff_mutex);
  2151. /* Now do a reliable test... */
  2152. if (!sb_has_quota_loaded(sb, type)) {
  2153. mutex_unlock(&dqopt->dqonoff_mutex);
  2154. goto load_quota;
  2155. }
  2156. if (flags & DQUOT_USAGE_ENABLED &&
  2157. sb_has_quota_usage_enabled(sb, type)) {
  2158. ret = -EBUSY;
  2159. goto out_lock;
  2160. }
  2161. if (flags & DQUOT_LIMITS_ENABLED &&
  2162. sb_has_quota_limits_enabled(sb, type)) {
  2163. ret = -EBUSY;
  2164. goto out_lock;
  2165. }
  2166. spin_lock(&dq_state_lock);
  2167. sb_dqopt(sb)->flags |= dquot_state_flag(flags, type);
  2168. spin_unlock(&dq_state_lock);
  2169. out_lock:
  2170. mutex_unlock(&dqopt->dqonoff_mutex);
  2171. return ret;
  2172. }
  2173. load_quota:
  2174. return vfs_load_quota_inode(inode, type, format_id, flags);
  2175. }
  2176. EXPORT_SYMBOL(dquot_enable);
  2177. /*
  2178. * This function is used when filesystem needs to initialize quotas
  2179. * during mount time.
  2180. */
  2181. int dquot_quota_on_mount(struct super_block *sb, char *qf_name,
  2182. int format_id, int type)
  2183. {
  2184. struct dentry *dentry;
  2185. int error;
  2186. mutex_lock(&d_inode(sb->s_root)->i_mutex);
  2187. dentry = lookup_one_len(qf_name, sb->s_root, strlen(qf_name));
  2188. mutex_unlock(&d_inode(sb->s_root)->i_mutex);
  2189. if (IS_ERR(dentry))
  2190. return PTR_ERR(dentry);
  2191. if (d_really_is_negative(dentry)) {
  2192. error = -ENOENT;
  2193. goto out;
  2194. }
  2195. error = security_quota_on(dentry);
  2196. if (!error)
  2197. error = vfs_load_quota_inode(d_inode(dentry), type, format_id,
  2198. DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED);
  2199. out:
  2200. dput(dentry);
  2201. return error;
  2202. }
  2203. EXPORT_SYMBOL(dquot_quota_on_mount);
  2204. static int dquot_quota_enable(struct super_block *sb, unsigned int flags)
  2205. {
  2206. int ret;
  2207. int type;
  2208. struct quota_info *dqopt = sb_dqopt(sb);
  2209. if (!(dqopt->flags & DQUOT_QUOTA_SYS_FILE))
  2210. return -ENOSYS;
  2211. /* Accounting cannot be turned on while fs is mounted */
  2212. flags &= ~(FS_QUOTA_UDQ_ACCT | FS_QUOTA_GDQ_ACCT | FS_QUOTA_PDQ_ACCT);
  2213. if (!flags)
  2214. return -EINVAL;
  2215. for (type = 0; type < MAXQUOTAS; type++) {
  2216. if (!(flags & qtype_enforce_flag(type)))
  2217. continue;
  2218. /* Can't enforce without accounting */
  2219. if (!sb_has_quota_usage_enabled(sb, type))
  2220. return -EINVAL;
  2221. ret = dquot_enable(dqopt->files[type], type,
  2222. dqopt->info[type].dqi_fmt_id,
  2223. DQUOT_LIMITS_ENABLED);
  2224. if (ret < 0)
  2225. goto out_err;
  2226. }
  2227. return 0;
  2228. out_err:
  2229. /* Backout enforcement enablement we already did */
  2230. for (type--; type >= 0; type--) {
  2231. if (flags & qtype_enforce_flag(type))
  2232. dquot_disable(sb, type, DQUOT_LIMITS_ENABLED);
  2233. }
  2234. /* Error code translation for better compatibility with XFS */
  2235. if (ret == -EBUSY)
  2236. ret = -EEXIST;
  2237. return ret;
  2238. }
  2239. static int dquot_quota_disable(struct super_block *sb, unsigned int flags)
  2240. {
  2241. int ret;
  2242. int type;
  2243. struct quota_info *dqopt = sb_dqopt(sb);
  2244. if (!(dqopt->flags & DQUOT_QUOTA_SYS_FILE))
  2245. return -ENOSYS;
  2246. /*
  2247. * We don't support turning off accounting via quotactl. In principle
  2248. * quota infrastructure can do this but filesystems don't expect
  2249. * userspace to be able to do it.
  2250. */
  2251. if (flags &
  2252. (FS_QUOTA_UDQ_ACCT | FS_QUOTA_GDQ_ACCT | FS_QUOTA_PDQ_ACCT))
  2253. return -EOPNOTSUPP;
  2254. /* Filter out limits not enabled */
  2255. for (type = 0; type < MAXQUOTAS; type++)
  2256. if (!sb_has_quota_limits_enabled(sb, type))
  2257. flags &= ~qtype_enforce_flag(type);
  2258. /* Nothing left? */
  2259. if (!flags)
  2260. return -EEXIST;
  2261. for (type = 0; type < MAXQUOTAS; type++) {
  2262. if (flags & qtype_enforce_flag(type)) {
  2263. ret = dquot_disable(sb, type, DQUOT_LIMITS_ENABLED);
  2264. if (ret < 0)
  2265. goto out_err;
  2266. }
  2267. }
  2268. return 0;
  2269. out_err:
  2270. /* Backout enforcement disabling we already did */
  2271. for (type--; type >= 0; type--) {
  2272. if (flags & qtype_enforce_flag(type))
  2273. dquot_enable(dqopt->files[type], type,
  2274. dqopt->info[type].dqi_fmt_id,
  2275. DQUOT_LIMITS_ENABLED);
  2276. }
  2277. return ret;
  2278. }
  2279. /* Generic routine for getting common part of quota structure */
  2280. static void do_get_dqblk(struct dquot *dquot, struct qc_dqblk *di)
  2281. {
  2282. struct mem_dqblk *dm = &dquot->dq_dqb;
  2283. memset(di, 0, sizeof(*di));
  2284. spin_lock(&dq_data_lock);
  2285. di->d_spc_hardlimit = dm->dqb_bhardlimit;
  2286. di->d_spc_softlimit = dm->dqb_bsoftlimit;
  2287. di->d_ino_hardlimit = dm->dqb_ihardlimit;
  2288. di->d_ino_softlimit = dm->dqb_isoftlimit;
  2289. di->d_space = dm->dqb_curspace + dm->dqb_rsvspace;
  2290. di->d_ino_count = dm->dqb_curinodes;
  2291. di->d_spc_timer = dm->dqb_btime;
  2292. di->d_ino_timer = dm->dqb_itime;
  2293. spin_unlock(&dq_data_lock);
  2294. }
  2295. int dquot_get_dqblk(struct super_block *sb, struct kqid qid,
  2296. struct qc_dqblk *di)
  2297. {
  2298. struct dquot *dquot;
  2299. dquot = dqget(sb, qid);
  2300. if (IS_ERR(dquot))
  2301. return PTR_ERR(dquot);
  2302. do_get_dqblk(dquot, di);
  2303. dqput(dquot);
  2304. return 0;
  2305. }
  2306. EXPORT_SYMBOL(dquot_get_dqblk);
  2307. #define VFS_QC_MASK \
  2308. (QC_SPACE | QC_SPC_SOFT | QC_SPC_HARD | \
  2309. QC_INO_COUNT | QC_INO_SOFT | QC_INO_HARD | \
  2310. QC_SPC_TIMER | QC_INO_TIMER)
  2311. /* Generic routine for setting common part of quota structure */
  2312. static int do_set_dqblk(struct dquot *dquot, struct qc_dqblk *di)
  2313. {
  2314. struct mem_dqblk *dm = &dquot->dq_dqb;
  2315. int check_blim = 0, check_ilim = 0;
  2316. struct mem_dqinfo *dqi = &sb_dqopt(dquot->dq_sb)->info[dquot->dq_id.type];
  2317. if (di->d_fieldmask & ~VFS_QC_MASK)
  2318. return -EINVAL;
  2319. if (((di->d_fieldmask & QC_SPC_SOFT) &&
  2320. di->d_spc_softlimit > dqi->dqi_max_spc_limit) ||
  2321. ((di->d_fieldmask & QC_SPC_HARD) &&
  2322. di->d_spc_hardlimit > dqi->dqi_max_spc_limit) ||
  2323. ((di->d_fieldmask & QC_INO_SOFT) &&
  2324. (di->d_ino_softlimit > dqi->dqi_max_ino_limit)) ||
  2325. ((di->d_fieldmask & QC_INO_HARD) &&
  2326. (di->d_ino_hardlimit > dqi->dqi_max_ino_limit)))
  2327. return -ERANGE;
  2328. spin_lock(&dq_data_lock);
  2329. if (di->d_fieldmask & QC_SPACE) {
  2330. dm->dqb_curspace = di->d_space - dm->dqb_rsvspace;
  2331. check_blim = 1;
  2332. set_bit(DQ_LASTSET_B + QIF_SPACE_B, &dquot->dq_flags);
  2333. }
  2334. if (di->d_fieldmask & QC_SPC_SOFT)
  2335. dm->dqb_bsoftlimit = di->d_spc_softlimit;
  2336. if (di->d_fieldmask & QC_SPC_HARD)
  2337. dm->dqb_bhardlimit = di->d_spc_hardlimit;
  2338. if (di->d_fieldmask & (QC_SPC_SOFT | QC_SPC_HARD)) {
  2339. check_blim = 1;
  2340. set_bit(DQ_LASTSET_B + QIF_BLIMITS_B, &dquot->dq_flags);
  2341. }
  2342. if (di->d_fieldmask & QC_INO_COUNT) {
  2343. dm->dqb_curinodes = di->d_ino_count;
  2344. check_ilim = 1;
  2345. set_bit(DQ_LASTSET_B + QIF_INODES_B, &dquot->dq_flags);
  2346. }
  2347. if (di->d_fieldmask & QC_INO_SOFT)
  2348. dm->dqb_isoftlimit = di->d_ino_softlimit;
  2349. if (di->d_fieldmask & QC_INO_HARD)
  2350. dm->dqb_ihardlimit = di->d_ino_hardlimit;
  2351. if (di->d_fieldmask & (QC_INO_SOFT | QC_INO_HARD)) {
  2352. check_ilim = 1;
  2353. set_bit(DQ_LASTSET_B + QIF_ILIMITS_B, &dquot->dq_flags);
  2354. }
  2355. if (di->d_fieldmask & QC_SPC_TIMER) {
  2356. dm->dqb_btime = di->d_spc_timer;
  2357. check_blim = 1;
  2358. set_bit(DQ_LASTSET_B + QIF_BTIME_B, &dquot->dq_flags);
  2359. }
  2360. if (di->d_fieldmask & QC_INO_TIMER) {
  2361. dm->dqb_itime = di->d_ino_timer;
  2362. check_ilim = 1;
  2363. set_bit(DQ_LASTSET_B + QIF_ITIME_B, &dquot->dq_flags);
  2364. }
  2365. if (check_blim) {
  2366. if (!dm->dqb_bsoftlimit ||
  2367. dm->dqb_curspace < dm->dqb_bsoftlimit) {
  2368. dm->dqb_btime = 0;
  2369. clear_bit(DQ_BLKS_B, &dquot->dq_flags);
  2370. } else if (!(di->d_fieldmask & QC_SPC_TIMER))
  2371. /* Set grace only if user hasn't provided his own... */
  2372. dm->dqb_btime = get_seconds() + dqi->dqi_bgrace;
  2373. }
  2374. if (check_ilim) {
  2375. if (!dm->dqb_isoftlimit ||
  2376. dm->dqb_curinodes < dm->dqb_isoftlimit) {
  2377. dm->dqb_itime = 0;
  2378. clear_bit(DQ_INODES_B, &dquot->dq_flags);
  2379. } else if (!(di->d_fieldmask & QC_INO_TIMER))
  2380. /* Set grace only if user hasn't provided his own... */
  2381. dm->dqb_itime = get_seconds() + dqi->dqi_igrace;
  2382. }
  2383. if (dm->dqb_bhardlimit || dm->dqb_bsoftlimit || dm->dqb_ihardlimit ||
  2384. dm->dqb_isoftlimit)
  2385. clear_bit(DQ_FAKE_B, &dquot->dq_flags);
  2386. else
  2387. set_bit(DQ_FAKE_B, &dquot->dq_flags);
  2388. spin_unlock(&dq_data_lock);
  2389. mark_dquot_dirty(dquot);
  2390. return 0;
  2391. }
  2392. int dquot_set_dqblk(struct super_block *sb, struct kqid qid,
  2393. struct qc_dqblk *di)
  2394. {
  2395. struct dquot *dquot;
  2396. int rc;
  2397. dquot = dqget(sb, qid);
  2398. if (IS_ERR(dquot)) {
  2399. rc = PTR_ERR(dquot);
  2400. goto out;
  2401. }
  2402. rc = do_set_dqblk(dquot, di);
  2403. dqput(dquot);
  2404. out:
  2405. return rc;
  2406. }
  2407. EXPORT_SYMBOL(dquot_set_dqblk);
  2408. /* Generic routine for getting common part of quota file information */
  2409. int dquot_get_state(struct super_block *sb, struct qc_state *state)
  2410. {
  2411. struct mem_dqinfo *mi;
  2412. struct qc_type_state *tstate;
  2413. struct quota_info *dqopt = sb_dqopt(sb);
  2414. int type;
  2415. mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
  2416. memset(state, 0, sizeof(*state));
  2417. for (type = 0; type < MAXQUOTAS; type++) {
  2418. if (!sb_has_quota_active(sb, type))
  2419. continue;
  2420. tstate = state->s_state + type;
  2421. mi = sb_dqopt(sb)->info + type;
  2422. tstate->flags = QCI_ACCT_ENABLED;
  2423. spin_lock(&dq_data_lock);
  2424. if (mi->dqi_flags & DQF_SYS_FILE)
  2425. tstate->flags |= QCI_SYSFILE;
  2426. if (mi->dqi_flags & DQF_ROOT_SQUASH)
  2427. tstate->flags |= QCI_ROOT_SQUASH;
  2428. if (sb_has_quota_limits_enabled(sb, type))
  2429. tstate->flags |= QCI_LIMITS_ENFORCED;
  2430. tstate->spc_timelimit = mi->dqi_bgrace;
  2431. tstate->ino_timelimit = mi->dqi_igrace;
  2432. tstate->ino = dqopt->files[type]->i_ino;
  2433. tstate->blocks = dqopt->files[type]->i_blocks;
  2434. tstate->nextents = 1; /* We don't know... */
  2435. spin_unlock(&dq_data_lock);
  2436. }
  2437. mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
  2438. return 0;
  2439. }
  2440. EXPORT_SYMBOL(dquot_get_state);
  2441. /* Generic routine for setting common part of quota file information */
  2442. int dquot_set_dqinfo(struct super_block *sb, int type, struct qc_info *ii)
  2443. {
  2444. struct mem_dqinfo *mi;
  2445. int err = 0;
  2446. if ((ii->i_fieldmask & QC_WARNS_MASK) ||
  2447. (ii->i_fieldmask & QC_RT_SPC_TIMER))
  2448. return -EINVAL;
  2449. mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
  2450. if (!sb_has_quota_active(sb, type)) {
  2451. err = -ESRCH;
  2452. goto out;
  2453. }
  2454. mi = sb_dqopt(sb)->info + type;
  2455. if (ii->i_fieldmask & QC_FLAGS) {
  2456. if ((ii->i_flags & QCI_ROOT_SQUASH &&
  2457. mi->dqi_format->qf_fmt_id != QFMT_VFS_OLD)) {
  2458. err = -EINVAL;
  2459. goto out;
  2460. }
  2461. }
  2462. spin_lock(&dq_data_lock);
  2463. if (ii->i_fieldmask & QC_SPC_TIMER)
  2464. mi->dqi_bgrace = ii->i_spc_timelimit;
  2465. if (ii->i_fieldmask & QC_INO_TIMER)
  2466. mi->dqi_igrace = ii->i_ino_timelimit;
  2467. if (ii->i_fieldmask & QC_FLAGS) {
  2468. if (ii->i_flags & QCI_ROOT_SQUASH)
  2469. mi->dqi_flags |= DQF_ROOT_SQUASH;
  2470. else
  2471. mi->dqi_flags &= ~DQF_ROOT_SQUASH;
  2472. }
  2473. spin_unlock(&dq_data_lock);
  2474. mark_info_dirty(sb, type);
  2475. /* Force write to disk */
  2476. sb->dq_op->write_info(sb, type);
  2477. out:
  2478. mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
  2479. return err;
  2480. }
  2481. EXPORT_SYMBOL(dquot_set_dqinfo);
  2482. const struct quotactl_ops dquot_quotactl_ops = {
  2483. .quota_on = dquot_quota_on,
  2484. .quota_off = dquot_quota_off,
  2485. .quota_sync = dquot_quota_sync,
  2486. .get_state = dquot_get_state,
  2487. .set_info = dquot_set_dqinfo,
  2488. .get_dqblk = dquot_get_dqblk,
  2489. .set_dqblk = dquot_set_dqblk
  2490. };
  2491. EXPORT_SYMBOL(dquot_quotactl_ops);
  2492. const struct quotactl_ops dquot_quotactl_sysfile_ops = {
  2493. .quota_enable = dquot_quota_enable,
  2494. .quota_disable = dquot_quota_disable,
  2495. .quota_sync = dquot_quota_sync,
  2496. .get_state = dquot_get_state,
  2497. .set_info = dquot_set_dqinfo,
  2498. .get_dqblk = dquot_get_dqblk,
  2499. .set_dqblk = dquot_set_dqblk
  2500. };
  2501. EXPORT_SYMBOL(dquot_quotactl_sysfile_ops);
  2502. static int do_proc_dqstats(struct ctl_table *table, int write,
  2503. void __user *buffer, size_t *lenp, loff_t *ppos)
  2504. {
  2505. unsigned int type = (int *)table->data - dqstats.stat;
  2506. /* Update global table */
  2507. dqstats.stat[type] =
  2508. percpu_counter_sum_positive(&dqstats.counter[type]);
  2509. return proc_dointvec(table, write, buffer, lenp, ppos);
  2510. }
  2511. static struct ctl_table fs_dqstats_table[] = {
  2512. {
  2513. .procname = "lookups",
  2514. .data = &dqstats.stat[DQST_LOOKUPS],
  2515. .maxlen = sizeof(int),
  2516. .mode = 0444,
  2517. .proc_handler = do_proc_dqstats,
  2518. },
  2519. {
  2520. .procname = "drops",
  2521. .data = &dqstats.stat[DQST_DROPS],
  2522. .maxlen = sizeof(int),
  2523. .mode = 0444,
  2524. .proc_handler = do_proc_dqstats,
  2525. },
  2526. {
  2527. .procname = "reads",
  2528. .data = &dqstats.stat[DQST_READS],
  2529. .maxlen = sizeof(int),
  2530. .mode = 0444,
  2531. .proc_handler = do_proc_dqstats,
  2532. },
  2533. {
  2534. .procname = "writes",
  2535. .data = &dqstats.stat[DQST_WRITES],
  2536. .maxlen = sizeof(int),
  2537. .mode = 0444,
  2538. .proc_handler = do_proc_dqstats,
  2539. },
  2540. {
  2541. .procname = "cache_hits",
  2542. .data = &dqstats.stat[DQST_CACHE_HITS],
  2543. .maxlen = sizeof(int),
  2544. .mode = 0444,
  2545. .proc_handler = do_proc_dqstats,
  2546. },
  2547. {
  2548. .procname = "allocated_dquots",
  2549. .data = &dqstats.stat[DQST_ALLOC_DQUOTS],
  2550. .maxlen = sizeof(int),
  2551. .mode = 0444,
  2552. .proc_handler = do_proc_dqstats,
  2553. },
  2554. {
  2555. .procname = "free_dquots",
  2556. .data = &dqstats.stat[DQST_FREE_DQUOTS],
  2557. .maxlen = sizeof(int),
  2558. .mode = 0444,
  2559. .proc_handler = do_proc_dqstats,
  2560. },
  2561. {
  2562. .procname = "syncs",
  2563. .data = &dqstats.stat[DQST_SYNCS],
  2564. .maxlen = sizeof(int),
  2565. .mode = 0444,
  2566. .proc_handler = do_proc_dqstats,
  2567. },
  2568. #ifdef CONFIG_PRINT_QUOTA_WARNING
  2569. {
  2570. .procname = "warnings",
  2571. .data = &flag_print_warnings,
  2572. .maxlen = sizeof(int),
  2573. .mode = 0644,
  2574. .proc_handler = proc_dointvec,
  2575. },
  2576. #endif
  2577. { },
  2578. };
  2579. static struct ctl_table fs_table[] = {
  2580. {
  2581. .procname = "quota",
  2582. .mode = 0555,
  2583. .child = fs_dqstats_table,
  2584. },
  2585. { },
  2586. };
  2587. static struct ctl_table sys_table[] = {
  2588. {
  2589. .procname = "fs",
  2590. .mode = 0555,
  2591. .child = fs_table,
  2592. },
  2593. { },
  2594. };
  2595. static int __init dquot_init(void)
  2596. {
  2597. int i, ret;
  2598. unsigned long nr_hash, order;
  2599. printk(KERN_NOTICE "VFS: Disk quotas %s\n", __DQUOT_VERSION__);
  2600. register_sysctl_table(sys_table);
  2601. dquot_cachep = kmem_cache_create("dquot",
  2602. sizeof(struct dquot), sizeof(unsigned long) * 4,
  2603. (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
  2604. SLAB_MEM_SPREAD|SLAB_PANIC),
  2605. NULL);
  2606. order = 0;
  2607. dquot_hash = (struct hlist_head *)__get_free_pages(GFP_ATOMIC, order);
  2608. if (!dquot_hash)
  2609. panic("Cannot create dquot hash table");
  2610. for (i = 0; i < _DQST_DQSTAT_LAST; i++) {
  2611. ret = percpu_counter_init(&dqstats.counter[i], 0, GFP_KERNEL);
  2612. if (ret)
  2613. panic("Cannot create dquot stat counters");
  2614. }
  2615. /* Find power-of-two hlist_heads which can fit into allocation */
  2616. nr_hash = (1UL << order) * PAGE_SIZE / sizeof(struct hlist_head);
  2617. dq_hash_bits = 0;
  2618. do {
  2619. dq_hash_bits++;
  2620. } while (nr_hash >> dq_hash_bits);
  2621. dq_hash_bits--;
  2622. nr_hash = 1UL << dq_hash_bits;
  2623. dq_hash_mask = nr_hash - 1;
  2624. for (i = 0; i < nr_hash; i++)
  2625. INIT_HLIST_HEAD(dquot_hash + i);
  2626. pr_info("VFS: Dquot-cache hash table entries: %ld (order %ld,"
  2627. " %ld bytes)\n", nr_hash, order, (PAGE_SIZE << order));
  2628. register_shrinker(&dqcache_shrinker);
  2629. return 0;
  2630. }
  2631. module_init(dquot_init);