dquot.c 69 KB

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