dquot.c 70 KB

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