dquot.c 69 KB

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