dquot.c 68 KB

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