dquot.c 69 KB

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