dquot.c 71 KB

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