dquot.c 71 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567
  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. /*
  819. * Claim reserved quota space
  820. */
  821. static void dquot_claim_reserved_space(struct dquot *dquot,
  822. qsize_t number)
  823. {
  824. WARN_ON(dquot->dq_dqb.dqb_rsvspace < number);
  825. dquot->dq_dqb.dqb_curspace += number;
  826. dquot->dq_dqb.dqb_rsvspace -= number;
  827. }
  828. static inline
  829. void dquot_free_reserved_space(struct dquot *dquot, qsize_t number)
  830. {
  831. dquot->dq_dqb.dqb_rsvspace -= number;
  832. }
  833. static inline void dquot_decr_inodes(struct dquot *dquot, qsize_t number)
  834. {
  835. if (sb_dqopt(dquot->dq_sb)->flags & DQUOT_NEGATIVE_USAGE ||
  836. dquot->dq_dqb.dqb_curinodes >= number)
  837. dquot->dq_dqb.dqb_curinodes -= number;
  838. else
  839. dquot->dq_dqb.dqb_curinodes = 0;
  840. if (dquot->dq_dqb.dqb_curinodes <= dquot->dq_dqb.dqb_isoftlimit)
  841. dquot->dq_dqb.dqb_itime = (time_t) 0;
  842. clear_bit(DQ_INODES_B, &dquot->dq_flags);
  843. }
  844. static inline void dquot_decr_space(struct dquot *dquot, qsize_t number)
  845. {
  846. if (sb_dqopt(dquot->dq_sb)->flags & DQUOT_NEGATIVE_USAGE ||
  847. dquot->dq_dqb.dqb_curspace >= number)
  848. dquot->dq_dqb.dqb_curspace -= number;
  849. else
  850. dquot->dq_dqb.dqb_curspace = 0;
  851. if (dquot->dq_dqb.dqb_curspace <= dquot->dq_dqb.dqb_bsoftlimit)
  852. dquot->dq_dqb.dqb_btime = (time_t) 0;
  853. clear_bit(DQ_BLKS_B, &dquot->dq_flags);
  854. }
  855. static int warning_issued(struct dquot *dquot, const int warntype)
  856. {
  857. int flag = (warntype == QUOTA_NL_BHARDWARN ||
  858. warntype == QUOTA_NL_BSOFTLONGWARN) ? DQ_BLKS_B :
  859. ((warntype == QUOTA_NL_IHARDWARN ||
  860. warntype == QUOTA_NL_ISOFTLONGWARN) ? DQ_INODES_B : 0);
  861. if (!flag)
  862. return 0;
  863. return test_and_set_bit(flag, &dquot->dq_flags);
  864. }
  865. #ifdef CONFIG_PRINT_QUOTA_WARNING
  866. static int flag_print_warnings = 1;
  867. static inline int need_print_warning(struct dquot *dquot)
  868. {
  869. if (!flag_print_warnings)
  870. return 0;
  871. switch (dquot->dq_type) {
  872. case USRQUOTA:
  873. return current_fsuid() == dquot->dq_id;
  874. case GRPQUOTA:
  875. return in_group_p(dquot->dq_id);
  876. }
  877. return 0;
  878. }
  879. /* Print warning to user which exceeded quota */
  880. static void print_warning(struct dquot *dquot, const int warntype)
  881. {
  882. char *msg = NULL;
  883. struct tty_struct *tty;
  884. if (warntype == QUOTA_NL_IHARDBELOW ||
  885. warntype == QUOTA_NL_ISOFTBELOW ||
  886. warntype == QUOTA_NL_BHARDBELOW ||
  887. warntype == QUOTA_NL_BSOFTBELOW || !need_print_warning(dquot))
  888. return;
  889. tty = get_current_tty();
  890. if (!tty)
  891. return;
  892. tty_write_message(tty, dquot->dq_sb->s_id);
  893. if (warntype == QUOTA_NL_ISOFTWARN || warntype == QUOTA_NL_BSOFTWARN)
  894. tty_write_message(tty, ": warning, ");
  895. else
  896. tty_write_message(tty, ": write failed, ");
  897. tty_write_message(tty, quotatypes[dquot->dq_type]);
  898. switch (warntype) {
  899. case QUOTA_NL_IHARDWARN:
  900. msg = " file limit reached.\r\n";
  901. break;
  902. case QUOTA_NL_ISOFTLONGWARN:
  903. msg = " file quota exceeded too long.\r\n";
  904. break;
  905. case QUOTA_NL_ISOFTWARN:
  906. msg = " file quota exceeded.\r\n";
  907. break;
  908. case QUOTA_NL_BHARDWARN:
  909. msg = " block limit reached.\r\n";
  910. break;
  911. case QUOTA_NL_BSOFTLONGWARN:
  912. msg = " block quota exceeded too long.\r\n";
  913. break;
  914. case QUOTA_NL_BSOFTWARN:
  915. msg = " block quota exceeded.\r\n";
  916. break;
  917. }
  918. tty_write_message(tty, msg);
  919. tty_kref_put(tty);
  920. }
  921. #endif
  922. #ifdef CONFIG_QUOTA_NETLINK_INTERFACE
  923. /* Netlink family structure for quota */
  924. static struct genl_family quota_genl_family = {
  925. .id = GENL_ID_GENERATE,
  926. .hdrsize = 0,
  927. .name = "VFS_DQUOT",
  928. .version = 1,
  929. .maxattr = QUOTA_NL_A_MAX,
  930. };
  931. /* Send warning to userspace about user which exceeded quota */
  932. static void send_warning(const struct dquot *dquot, const char warntype)
  933. {
  934. static atomic_t seq;
  935. struct sk_buff *skb;
  936. void *msg_head;
  937. int ret;
  938. int msg_size = 4 * nla_total_size(sizeof(u32)) +
  939. 2 * nla_total_size(sizeof(u64));
  940. /* We have to allocate using GFP_NOFS as we are called from a
  941. * filesystem performing write and thus further recursion into
  942. * the fs to free some data could cause deadlocks. */
  943. skb = genlmsg_new(msg_size, GFP_NOFS);
  944. if (!skb) {
  945. printk(KERN_ERR
  946. "VFS: Not enough memory to send quota warning.\n");
  947. return;
  948. }
  949. msg_head = genlmsg_put(skb, 0, atomic_add_return(1, &seq),
  950. &quota_genl_family, 0, QUOTA_NL_C_WARNING);
  951. if (!msg_head) {
  952. printk(KERN_ERR
  953. "VFS: Cannot store netlink header in quota warning.\n");
  954. goto err_out;
  955. }
  956. ret = nla_put_u32(skb, QUOTA_NL_A_QTYPE, dquot->dq_type);
  957. if (ret)
  958. goto attr_err_out;
  959. ret = nla_put_u64(skb, QUOTA_NL_A_EXCESS_ID, dquot->dq_id);
  960. if (ret)
  961. goto attr_err_out;
  962. ret = nla_put_u32(skb, QUOTA_NL_A_WARNING, warntype);
  963. if (ret)
  964. goto attr_err_out;
  965. ret = nla_put_u32(skb, QUOTA_NL_A_DEV_MAJOR,
  966. MAJOR(dquot->dq_sb->s_dev));
  967. if (ret)
  968. goto attr_err_out;
  969. ret = nla_put_u32(skb, QUOTA_NL_A_DEV_MINOR,
  970. MINOR(dquot->dq_sb->s_dev));
  971. if (ret)
  972. goto attr_err_out;
  973. ret = nla_put_u64(skb, QUOTA_NL_A_CAUSED_ID, current_uid());
  974. if (ret)
  975. goto attr_err_out;
  976. genlmsg_end(skb, msg_head);
  977. ret = genlmsg_multicast(skb, 0, quota_genl_family.id, GFP_NOFS);
  978. if (ret < 0 && ret != -ESRCH)
  979. printk(KERN_ERR
  980. "VFS: Failed to send notification message: %d\n", ret);
  981. return;
  982. attr_err_out:
  983. printk(KERN_ERR "VFS: Not enough space to compose quota message!\n");
  984. err_out:
  985. kfree_skb(skb);
  986. }
  987. #endif
  988. /*
  989. * Write warnings to the console and send warning messages over netlink.
  990. *
  991. * Note that this function can sleep.
  992. */
  993. static inline void flush_warnings(struct dquot * const *dquots, char *warntype)
  994. {
  995. int i;
  996. for (i = 0; i < MAXQUOTAS; i++)
  997. if (dquots[i] != NODQUOT && warntype[i] != QUOTA_NL_NOWARN &&
  998. !warning_issued(dquots[i], warntype[i])) {
  999. #ifdef CONFIG_PRINT_QUOTA_WARNING
  1000. print_warning(dquots[i], warntype[i]);
  1001. #endif
  1002. #ifdef CONFIG_QUOTA_NETLINK_INTERFACE
  1003. send_warning(dquots[i], warntype[i]);
  1004. #endif
  1005. }
  1006. }
  1007. static inline char ignore_hardlimit(struct dquot *dquot)
  1008. {
  1009. struct mem_dqinfo *info = &sb_dqopt(dquot->dq_sb)->info[dquot->dq_type];
  1010. return capable(CAP_SYS_RESOURCE) &&
  1011. (info->dqi_format->qf_fmt_id != QFMT_VFS_OLD || !(info->dqi_flags & V1_DQF_RSQUASH));
  1012. }
  1013. /* needs dq_data_lock */
  1014. static int check_idq(struct dquot *dquot, qsize_t inodes, char *warntype)
  1015. {
  1016. *warntype = QUOTA_NL_NOWARN;
  1017. if (!sb_has_quota_limits_enabled(dquot->dq_sb, dquot->dq_type) ||
  1018. test_bit(DQ_FAKE_B, &dquot->dq_flags))
  1019. return QUOTA_OK;
  1020. if (dquot->dq_dqb.dqb_ihardlimit &&
  1021. (dquot->dq_dqb.dqb_curinodes + inodes) > dquot->dq_dqb.dqb_ihardlimit &&
  1022. !ignore_hardlimit(dquot)) {
  1023. *warntype = QUOTA_NL_IHARDWARN;
  1024. return NO_QUOTA;
  1025. }
  1026. if (dquot->dq_dqb.dqb_isoftlimit &&
  1027. (dquot->dq_dqb.dqb_curinodes + inodes) > dquot->dq_dqb.dqb_isoftlimit &&
  1028. dquot->dq_dqb.dqb_itime && get_seconds() >= dquot->dq_dqb.dqb_itime &&
  1029. !ignore_hardlimit(dquot)) {
  1030. *warntype = QUOTA_NL_ISOFTLONGWARN;
  1031. return NO_QUOTA;
  1032. }
  1033. if (dquot->dq_dqb.dqb_isoftlimit &&
  1034. (dquot->dq_dqb.dqb_curinodes + inodes) > dquot->dq_dqb.dqb_isoftlimit &&
  1035. dquot->dq_dqb.dqb_itime == 0) {
  1036. *warntype = QUOTA_NL_ISOFTWARN;
  1037. dquot->dq_dqb.dqb_itime = get_seconds() + sb_dqopt(dquot->dq_sb)->info[dquot->dq_type].dqi_igrace;
  1038. }
  1039. return QUOTA_OK;
  1040. }
  1041. /* needs dq_data_lock */
  1042. static int check_bdq(struct dquot *dquot, qsize_t space, int prealloc, char *warntype)
  1043. {
  1044. qsize_t tspace;
  1045. *warntype = QUOTA_NL_NOWARN;
  1046. if (!sb_has_quota_limits_enabled(dquot->dq_sb, dquot->dq_type) ||
  1047. test_bit(DQ_FAKE_B, &dquot->dq_flags))
  1048. return QUOTA_OK;
  1049. tspace = dquot->dq_dqb.dqb_curspace + dquot->dq_dqb.dqb_rsvspace
  1050. + space;
  1051. if (dquot->dq_dqb.dqb_bhardlimit &&
  1052. tspace > dquot->dq_dqb.dqb_bhardlimit &&
  1053. !ignore_hardlimit(dquot)) {
  1054. if (!prealloc)
  1055. *warntype = QUOTA_NL_BHARDWARN;
  1056. return NO_QUOTA;
  1057. }
  1058. if (dquot->dq_dqb.dqb_bsoftlimit &&
  1059. tspace > dquot->dq_dqb.dqb_bsoftlimit &&
  1060. dquot->dq_dqb.dqb_btime && get_seconds() >= dquot->dq_dqb.dqb_btime &&
  1061. !ignore_hardlimit(dquot)) {
  1062. if (!prealloc)
  1063. *warntype = QUOTA_NL_BSOFTLONGWARN;
  1064. return NO_QUOTA;
  1065. }
  1066. if (dquot->dq_dqb.dqb_bsoftlimit &&
  1067. tspace > dquot->dq_dqb.dqb_bsoftlimit &&
  1068. dquot->dq_dqb.dqb_btime == 0) {
  1069. if (!prealloc) {
  1070. *warntype = QUOTA_NL_BSOFTWARN;
  1071. dquot->dq_dqb.dqb_btime = get_seconds() + sb_dqopt(dquot->dq_sb)->info[dquot->dq_type].dqi_bgrace;
  1072. }
  1073. else
  1074. /*
  1075. * We don't allow preallocation to exceed softlimit so exceeding will
  1076. * be always printed
  1077. */
  1078. return NO_QUOTA;
  1079. }
  1080. return QUOTA_OK;
  1081. }
  1082. static int info_idq_free(struct dquot *dquot, qsize_t inodes)
  1083. {
  1084. if (test_bit(DQ_FAKE_B, &dquot->dq_flags) ||
  1085. dquot->dq_dqb.dqb_curinodes <= dquot->dq_dqb.dqb_isoftlimit ||
  1086. !sb_has_quota_limits_enabled(dquot->dq_sb, dquot->dq_type))
  1087. return QUOTA_NL_NOWARN;
  1088. if (dquot->dq_dqb.dqb_curinodes - inodes <= dquot->dq_dqb.dqb_isoftlimit)
  1089. return QUOTA_NL_ISOFTBELOW;
  1090. if (dquot->dq_dqb.dqb_curinodes >= dquot->dq_dqb.dqb_ihardlimit &&
  1091. dquot->dq_dqb.dqb_curinodes - inodes < dquot->dq_dqb.dqb_ihardlimit)
  1092. return QUOTA_NL_IHARDBELOW;
  1093. return QUOTA_NL_NOWARN;
  1094. }
  1095. static int info_bdq_free(struct dquot *dquot, qsize_t space)
  1096. {
  1097. if (test_bit(DQ_FAKE_B, &dquot->dq_flags) ||
  1098. dquot->dq_dqb.dqb_curspace <= dquot->dq_dqb.dqb_bsoftlimit)
  1099. return QUOTA_NL_NOWARN;
  1100. if (dquot->dq_dqb.dqb_curspace - space <= dquot->dq_dqb.dqb_bsoftlimit)
  1101. return QUOTA_NL_BSOFTBELOW;
  1102. if (dquot->dq_dqb.dqb_curspace >= dquot->dq_dqb.dqb_bhardlimit &&
  1103. dquot->dq_dqb.dqb_curspace - space < dquot->dq_dqb.dqb_bhardlimit)
  1104. return QUOTA_NL_BHARDBELOW;
  1105. return QUOTA_NL_NOWARN;
  1106. }
  1107. /*
  1108. * Initialize quota pointers in inode
  1109. * We do things in a bit complicated way but by that we avoid calling
  1110. * dqget() and thus filesystem callbacks under dqptr_sem.
  1111. */
  1112. int dquot_initialize(struct inode *inode, int type)
  1113. {
  1114. unsigned int id = 0;
  1115. int cnt, ret = 0;
  1116. struct dquot *got[MAXQUOTAS] = { NODQUOT, NODQUOT };
  1117. struct super_block *sb = inode->i_sb;
  1118. /* First test before acquiring mutex - solves deadlocks when we
  1119. * re-enter the quota code and are already holding the mutex */
  1120. if (IS_NOQUOTA(inode))
  1121. return 0;
  1122. /* First get references to structures we might need. */
  1123. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1124. if (type != -1 && cnt != type)
  1125. continue;
  1126. switch (cnt) {
  1127. case USRQUOTA:
  1128. id = inode->i_uid;
  1129. break;
  1130. case GRPQUOTA:
  1131. id = inode->i_gid;
  1132. break;
  1133. }
  1134. got[cnt] = dqget(sb, id, cnt);
  1135. }
  1136. down_write(&sb_dqopt(sb)->dqptr_sem);
  1137. /* Having dqptr_sem we know NOQUOTA flags can't be altered... */
  1138. if (IS_NOQUOTA(inode))
  1139. goto out_err;
  1140. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1141. if (type != -1 && cnt != type)
  1142. continue;
  1143. /* Avoid races with quotaoff() */
  1144. if (!sb_has_quota_active(sb, cnt))
  1145. continue;
  1146. if (inode->i_dquot[cnt] == NODQUOT) {
  1147. inode->i_dquot[cnt] = got[cnt];
  1148. got[cnt] = NODQUOT;
  1149. }
  1150. }
  1151. out_err:
  1152. up_write(&sb_dqopt(sb)->dqptr_sem);
  1153. /* Drop unused references */
  1154. for (cnt = 0; cnt < MAXQUOTAS; cnt++)
  1155. dqput(got[cnt]);
  1156. return ret;
  1157. }
  1158. /*
  1159. * Release all quotas referenced by inode
  1160. */
  1161. int dquot_drop(struct inode *inode)
  1162. {
  1163. int cnt;
  1164. struct dquot *put[MAXQUOTAS];
  1165. down_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1166. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1167. put[cnt] = inode->i_dquot[cnt];
  1168. inode->i_dquot[cnt] = NODQUOT;
  1169. }
  1170. up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1171. for (cnt = 0; cnt < MAXQUOTAS; cnt++)
  1172. dqput(put[cnt]);
  1173. return 0;
  1174. }
  1175. /* Wrapper to remove references to quota structures from inode */
  1176. void vfs_dq_drop(struct inode *inode)
  1177. {
  1178. /* Here we can get arbitrary inode from clear_inode() so we have
  1179. * to be careful. OTOH we don't need locking as quota operations
  1180. * are allowed to change only at mount time */
  1181. if (!IS_NOQUOTA(inode) && inode->i_sb && inode->i_sb->dq_op
  1182. && inode->i_sb->dq_op->drop) {
  1183. int cnt;
  1184. /* Test before calling to rule out calls from proc and such
  1185. * where we are not allowed to block. Note that this is
  1186. * actually reliable test even without the lock - the caller
  1187. * must assure that nobody can come after the DQUOT_DROP and
  1188. * add quota pointers back anyway */
  1189. for (cnt = 0; cnt < MAXQUOTAS; cnt++)
  1190. if (inode->i_dquot[cnt] != NODQUOT)
  1191. break;
  1192. if (cnt < MAXQUOTAS)
  1193. inode->i_sb->dq_op->drop(inode);
  1194. }
  1195. }
  1196. /*
  1197. * Following four functions update i_blocks+i_bytes fields and
  1198. * quota information (together with appropriate checks)
  1199. * NOTE: We absolutely rely on the fact that caller dirties
  1200. * the inode (usually macros in quotaops.h care about this) and
  1201. * holds a handle for the current transaction so that dquot write and
  1202. * inode write go into the same transaction.
  1203. */
  1204. /*
  1205. * This operation can block, but only after everything is updated
  1206. */
  1207. int __dquot_alloc_space(struct inode *inode, qsize_t number,
  1208. int warn, int reserve)
  1209. {
  1210. int cnt, ret = QUOTA_OK;
  1211. char warntype[MAXQUOTAS];
  1212. for (cnt = 0; cnt < MAXQUOTAS; cnt++)
  1213. warntype[cnt] = QUOTA_NL_NOWARN;
  1214. spin_lock(&dq_data_lock);
  1215. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1216. if (inode->i_dquot[cnt] == NODQUOT)
  1217. continue;
  1218. if (check_bdq(inode->i_dquot[cnt], number, warn, warntype+cnt)
  1219. == NO_QUOTA) {
  1220. ret = NO_QUOTA;
  1221. goto out_unlock;
  1222. }
  1223. }
  1224. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1225. if (inode->i_dquot[cnt] == NODQUOT)
  1226. continue;
  1227. if (reserve)
  1228. dquot_resv_space(inode->i_dquot[cnt], number);
  1229. else
  1230. dquot_incr_space(inode->i_dquot[cnt], number);
  1231. }
  1232. if (!reserve)
  1233. inode_add_bytes(inode, number);
  1234. out_unlock:
  1235. spin_unlock(&dq_data_lock);
  1236. flush_warnings(inode->i_dquot, warntype);
  1237. return ret;
  1238. }
  1239. int dquot_alloc_space(struct inode *inode, qsize_t number, int warn)
  1240. {
  1241. int cnt, ret = QUOTA_OK;
  1242. /*
  1243. * First test before acquiring mutex - solves deadlocks when we
  1244. * re-enter the quota code and are already holding the mutex
  1245. */
  1246. if (IS_NOQUOTA(inode)) {
  1247. inode_add_bytes(inode, number);
  1248. goto out;
  1249. }
  1250. down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1251. if (IS_NOQUOTA(inode)) {
  1252. inode_add_bytes(inode, number);
  1253. goto out_unlock;
  1254. }
  1255. ret = __dquot_alloc_space(inode, number, warn, 0);
  1256. if (ret == NO_QUOTA)
  1257. goto out_unlock;
  1258. /* Dirtify all the dquots - this can block when journalling */
  1259. for (cnt = 0; cnt < MAXQUOTAS; cnt++)
  1260. if (inode->i_dquot[cnt])
  1261. mark_dquot_dirty(inode->i_dquot[cnt]);
  1262. out_unlock:
  1263. up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1264. out:
  1265. return ret;
  1266. }
  1267. int dquot_reserve_space(struct inode *inode, qsize_t number, int warn)
  1268. {
  1269. int ret = QUOTA_OK;
  1270. if (IS_NOQUOTA(inode))
  1271. goto out;
  1272. down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1273. if (IS_NOQUOTA(inode))
  1274. goto out_unlock;
  1275. ret = __dquot_alloc_space(inode, number, warn, 1);
  1276. out_unlock:
  1277. up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1278. out:
  1279. return ret;
  1280. }
  1281. EXPORT_SYMBOL(dquot_reserve_space);
  1282. /*
  1283. * This operation can block, but only after everything is updated
  1284. */
  1285. int dquot_alloc_inode(const struct inode *inode, qsize_t number)
  1286. {
  1287. int cnt, ret = NO_QUOTA;
  1288. char warntype[MAXQUOTAS];
  1289. /* First test before acquiring mutex - solves deadlocks when we
  1290. * re-enter the quota code and are already holding the mutex */
  1291. if (IS_NOQUOTA(inode))
  1292. return QUOTA_OK;
  1293. for (cnt = 0; cnt < MAXQUOTAS; cnt++)
  1294. warntype[cnt] = QUOTA_NL_NOWARN;
  1295. down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1296. if (IS_NOQUOTA(inode)) {
  1297. up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1298. return QUOTA_OK;
  1299. }
  1300. spin_lock(&dq_data_lock);
  1301. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1302. if (inode->i_dquot[cnt] == NODQUOT)
  1303. continue;
  1304. if (check_idq(inode->i_dquot[cnt], number, warntype+cnt) == NO_QUOTA)
  1305. goto warn_put_all;
  1306. }
  1307. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1308. if (inode->i_dquot[cnt] == NODQUOT)
  1309. continue;
  1310. dquot_incr_inodes(inode->i_dquot[cnt], number);
  1311. }
  1312. ret = QUOTA_OK;
  1313. warn_put_all:
  1314. spin_unlock(&dq_data_lock);
  1315. if (ret == QUOTA_OK)
  1316. /* Dirtify all the dquots - this can block when journalling */
  1317. for (cnt = 0; cnt < MAXQUOTAS; cnt++)
  1318. if (inode->i_dquot[cnt])
  1319. mark_dquot_dirty(inode->i_dquot[cnt]);
  1320. flush_warnings(inode->i_dquot, warntype);
  1321. up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1322. return ret;
  1323. }
  1324. int dquot_claim_space(struct inode *inode, qsize_t number)
  1325. {
  1326. int cnt;
  1327. int ret = QUOTA_OK;
  1328. if (IS_NOQUOTA(inode)) {
  1329. inode_add_bytes(inode, number);
  1330. goto out;
  1331. }
  1332. down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1333. if (IS_NOQUOTA(inode)) {
  1334. up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1335. inode_add_bytes(inode, number);
  1336. goto out;
  1337. }
  1338. spin_lock(&dq_data_lock);
  1339. /* Claim reserved quotas to allocated quotas */
  1340. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1341. if (inode->i_dquot[cnt] != NODQUOT)
  1342. dquot_claim_reserved_space(inode->i_dquot[cnt],
  1343. number);
  1344. }
  1345. /* Update inode bytes */
  1346. inode_add_bytes(inode, number);
  1347. spin_unlock(&dq_data_lock);
  1348. /* Dirtify all the dquots - this can block when journalling */
  1349. for (cnt = 0; cnt < MAXQUOTAS; cnt++)
  1350. if (inode->i_dquot[cnt])
  1351. mark_dquot_dirty(inode->i_dquot[cnt]);
  1352. up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1353. out:
  1354. return ret;
  1355. }
  1356. EXPORT_SYMBOL(dquot_claim_space);
  1357. /*
  1358. * Release reserved quota space
  1359. */
  1360. void dquot_release_reserved_space(struct inode *inode, qsize_t number)
  1361. {
  1362. int cnt;
  1363. if (IS_NOQUOTA(inode))
  1364. goto out;
  1365. down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1366. if (IS_NOQUOTA(inode))
  1367. goto out_unlock;
  1368. spin_lock(&dq_data_lock);
  1369. /* Release reserved dquots */
  1370. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1371. if (inode->i_dquot[cnt] != NODQUOT)
  1372. dquot_free_reserved_space(inode->i_dquot[cnt], number);
  1373. }
  1374. spin_unlock(&dq_data_lock);
  1375. out_unlock:
  1376. up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1377. out:
  1378. return;
  1379. }
  1380. EXPORT_SYMBOL(dquot_release_reserved_space);
  1381. /*
  1382. * This operation can block, but only after everything is updated
  1383. */
  1384. int dquot_free_space(struct inode *inode, qsize_t number)
  1385. {
  1386. unsigned int cnt;
  1387. char warntype[MAXQUOTAS];
  1388. /* First test before acquiring mutex - solves deadlocks when we
  1389. * re-enter the quota code and are already holding the mutex */
  1390. if (IS_NOQUOTA(inode)) {
  1391. out_sub:
  1392. inode_sub_bytes(inode, number);
  1393. return QUOTA_OK;
  1394. }
  1395. down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1396. /* Now recheck reliably when holding dqptr_sem */
  1397. if (IS_NOQUOTA(inode)) {
  1398. up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1399. goto out_sub;
  1400. }
  1401. spin_lock(&dq_data_lock);
  1402. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1403. if (inode->i_dquot[cnt] == NODQUOT)
  1404. continue;
  1405. warntype[cnt] = info_bdq_free(inode->i_dquot[cnt], number);
  1406. dquot_decr_space(inode->i_dquot[cnt], number);
  1407. }
  1408. inode_sub_bytes(inode, number);
  1409. spin_unlock(&dq_data_lock);
  1410. /* Dirtify all the dquots - this can block when journalling */
  1411. for (cnt = 0; cnt < MAXQUOTAS; cnt++)
  1412. if (inode->i_dquot[cnt])
  1413. mark_dquot_dirty(inode->i_dquot[cnt]);
  1414. flush_warnings(inode->i_dquot, warntype);
  1415. up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1416. return QUOTA_OK;
  1417. }
  1418. /*
  1419. * This operation can block, but only after everything is updated
  1420. */
  1421. int dquot_free_inode(const struct inode *inode, qsize_t number)
  1422. {
  1423. unsigned int cnt;
  1424. char warntype[MAXQUOTAS];
  1425. /* First test before acquiring mutex - solves deadlocks when we
  1426. * re-enter the quota code and are already holding the mutex */
  1427. if (IS_NOQUOTA(inode))
  1428. return QUOTA_OK;
  1429. down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1430. /* Now recheck reliably when holding dqptr_sem */
  1431. if (IS_NOQUOTA(inode)) {
  1432. up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1433. return QUOTA_OK;
  1434. }
  1435. spin_lock(&dq_data_lock);
  1436. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1437. if (inode->i_dquot[cnt] == NODQUOT)
  1438. continue;
  1439. warntype[cnt] = info_idq_free(inode->i_dquot[cnt], number);
  1440. dquot_decr_inodes(inode->i_dquot[cnt], number);
  1441. }
  1442. spin_unlock(&dq_data_lock);
  1443. /* Dirtify all the dquots - this can block when journalling */
  1444. for (cnt = 0; cnt < MAXQUOTAS; cnt++)
  1445. if (inode->i_dquot[cnt])
  1446. mark_dquot_dirty(inode->i_dquot[cnt]);
  1447. flush_warnings(inode->i_dquot, warntype);
  1448. up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1449. return QUOTA_OK;
  1450. }
  1451. /*
  1452. * call back function, get reserved quota space from underlying fs
  1453. */
  1454. qsize_t dquot_get_reserved_space(struct inode *inode)
  1455. {
  1456. qsize_t reserved_space = 0;
  1457. if (sb_any_quota_active(inode->i_sb) &&
  1458. inode->i_sb->dq_op->get_reserved_space)
  1459. reserved_space = inode->i_sb->dq_op->get_reserved_space(inode);
  1460. return reserved_space;
  1461. }
  1462. /*
  1463. * Transfer the number of inode and blocks from one diskquota to an other.
  1464. *
  1465. * This operation can block, but only after everything is updated
  1466. * A transaction must be started when entering this function.
  1467. */
  1468. int dquot_transfer(struct inode *inode, struct iattr *iattr)
  1469. {
  1470. qsize_t space, cur_space;
  1471. qsize_t rsv_space = 0;
  1472. struct dquot *transfer_from[MAXQUOTAS];
  1473. struct dquot *transfer_to[MAXQUOTAS];
  1474. int cnt, ret = QUOTA_OK;
  1475. int chuid = iattr->ia_valid & ATTR_UID && inode->i_uid != iattr->ia_uid,
  1476. chgid = iattr->ia_valid & ATTR_GID && inode->i_gid != iattr->ia_gid;
  1477. char warntype_to[MAXQUOTAS];
  1478. char warntype_from_inodes[MAXQUOTAS], warntype_from_space[MAXQUOTAS];
  1479. /* First test before acquiring mutex - solves deadlocks when we
  1480. * re-enter the quota code and are already holding the mutex */
  1481. if (IS_NOQUOTA(inode))
  1482. return QUOTA_OK;
  1483. /* Initialize the arrays */
  1484. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1485. transfer_from[cnt] = NODQUOT;
  1486. transfer_to[cnt] = NODQUOT;
  1487. warntype_to[cnt] = QUOTA_NL_NOWARN;
  1488. switch (cnt) {
  1489. case USRQUOTA:
  1490. if (!chuid)
  1491. continue;
  1492. transfer_to[cnt] = dqget(inode->i_sb, iattr->ia_uid, cnt);
  1493. break;
  1494. case GRPQUOTA:
  1495. if (!chgid)
  1496. continue;
  1497. transfer_to[cnt] = dqget(inode->i_sb, iattr->ia_gid, cnt);
  1498. break;
  1499. }
  1500. }
  1501. down_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1502. /* Now recheck reliably when holding dqptr_sem */
  1503. if (IS_NOQUOTA(inode)) { /* File without quota accounting? */
  1504. up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1505. goto put_all;
  1506. }
  1507. spin_lock(&dq_data_lock);
  1508. cur_space = inode_get_bytes(inode);
  1509. rsv_space = dquot_get_reserved_space(inode);
  1510. space = cur_space + rsv_space;
  1511. /* Build the transfer_from list and check the limits */
  1512. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1513. if (transfer_to[cnt] == NODQUOT)
  1514. continue;
  1515. transfer_from[cnt] = inode->i_dquot[cnt];
  1516. if (check_idq(transfer_to[cnt], 1, warntype_to + cnt) ==
  1517. NO_QUOTA || check_bdq(transfer_to[cnt], space, 0,
  1518. warntype_to + cnt) == NO_QUOTA)
  1519. goto over_quota;
  1520. }
  1521. /*
  1522. * Finally perform the needed transfer from transfer_from to transfer_to
  1523. */
  1524. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1525. /*
  1526. * Skip changes for same uid or gid or for turned off quota-type.
  1527. */
  1528. if (transfer_to[cnt] == NODQUOT)
  1529. continue;
  1530. /* Due to IO error we might not have transfer_from[] structure */
  1531. if (transfer_from[cnt]) {
  1532. warntype_from_inodes[cnt] =
  1533. info_idq_free(transfer_from[cnt], 1);
  1534. warntype_from_space[cnt] =
  1535. info_bdq_free(transfer_from[cnt], space);
  1536. dquot_decr_inodes(transfer_from[cnt], 1);
  1537. dquot_decr_space(transfer_from[cnt], cur_space);
  1538. dquot_free_reserved_space(transfer_from[cnt],
  1539. rsv_space);
  1540. }
  1541. dquot_incr_inodes(transfer_to[cnt], 1);
  1542. dquot_incr_space(transfer_to[cnt], cur_space);
  1543. dquot_resv_space(transfer_to[cnt], rsv_space);
  1544. inode->i_dquot[cnt] = transfer_to[cnt];
  1545. }
  1546. spin_unlock(&dq_data_lock);
  1547. up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1548. /* Dirtify all the dquots - this can block when journalling */
  1549. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1550. if (transfer_from[cnt])
  1551. mark_dquot_dirty(transfer_from[cnt]);
  1552. if (transfer_to[cnt]) {
  1553. mark_dquot_dirty(transfer_to[cnt]);
  1554. /* The reference we got is transferred to the inode */
  1555. transfer_to[cnt] = NODQUOT;
  1556. }
  1557. }
  1558. warn_put_all:
  1559. flush_warnings(transfer_to, warntype_to);
  1560. flush_warnings(transfer_from, warntype_from_inodes);
  1561. flush_warnings(transfer_from, warntype_from_space);
  1562. put_all:
  1563. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1564. dqput(transfer_from[cnt]);
  1565. dqput(transfer_to[cnt]);
  1566. }
  1567. return ret;
  1568. over_quota:
  1569. spin_unlock(&dq_data_lock);
  1570. up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1571. /* Clear dquot pointers we don't want to dqput() */
  1572. for (cnt = 0; cnt < MAXQUOTAS; cnt++)
  1573. transfer_from[cnt] = NODQUOT;
  1574. ret = NO_QUOTA;
  1575. goto warn_put_all;
  1576. }
  1577. /* Wrapper for transferring ownership of an inode */
  1578. int vfs_dq_transfer(struct inode *inode, struct iattr *iattr)
  1579. {
  1580. if (sb_any_quota_active(inode->i_sb) && !IS_NOQUOTA(inode)) {
  1581. vfs_dq_init(inode);
  1582. if (inode->i_sb->dq_op->transfer(inode, iattr) == NO_QUOTA)
  1583. return 1;
  1584. }
  1585. return 0;
  1586. }
  1587. /*
  1588. * Write info of quota file to disk
  1589. */
  1590. int dquot_commit_info(struct super_block *sb, int type)
  1591. {
  1592. int ret;
  1593. struct quota_info *dqopt = sb_dqopt(sb);
  1594. mutex_lock(&dqopt->dqio_mutex);
  1595. ret = dqopt->ops[type]->write_file_info(sb, type);
  1596. mutex_unlock(&dqopt->dqio_mutex);
  1597. return ret;
  1598. }
  1599. /*
  1600. * Definitions of diskquota operations.
  1601. */
  1602. struct dquot_operations dquot_operations = {
  1603. .initialize = dquot_initialize,
  1604. .drop = dquot_drop,
  1605. .alloc_space = dquot_alloc_space,
  1606. .alloc_inode = dquot_alloc_inode,
  1607. .free_space = dquot_free_space,
  1608. .free_inode = dquot_free_inode,
  1609. .transfer = dquot_transfer,
  1610. .write_dquot = dquot_commit,
  1611. .acquire_dquot = dquot_acquire,
  1612. .release_dquot = dquot_release,
  1613. .mark_dirty = dquot_mark_dquot_dirty,
  1614. .write_info = dquot_commit_info,
  1615. .alloc_dquot = dquot_alloc,
  1616. .destroy_dquot = dquot_destroy,
  1617. };
  1618. /*
  1619. * Turn quota off on a device. type == -1 ==> quotaoff for all types (umount)
  1620. */
  1621. int vfs_quota_disable(struct super_block *sb, int type, unsigned int flags)
  1622. {
  1623. int cnt, ret = 0;
  1624. struct quota_info *dqopt = sb_dqopt(sb);
  1625. struct inode *toputinode[MAXQUOTAS];
  1626. /* Cannot turn off usage accounting without turning off limits, or
  1627. * suspend quotas and simultaneously turn quotas off. */
  1628. if ((flags & DQUOT_USAGE_ENABLED && !(flags & DQUOT_LIMITS_ENABLED))
  1629. || (flags & DQUOT_SUSPENDED && flags & (DQUOT_LIMITS_ENABLED |
  1630. DQUOT_USAGE_ENABLED)))
  1631. return -EINVAL;
  1632. /* We need to serialize quota_off() for device */
  1633. mutex_lock(&dqopt->dqonoff_mutex);
  1634. /*
  1635. * Skip everything if there's nothing to do. We have to do this because
  1636. * sometimes we are called when fill_super() failed and calling
  1637. * sync_fs() in such cases does no good.
  1638. */
  1639. if (!sb_any_quota_loaded(sb)) {
  1640. mutex_unlock(&dqopt->dqonoff_mutex);
  1641. return 0;
  1642. }
  1643. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1644. toputinode[cnt] = NULL;
  1645. if (type != -1 && cnt != type)
  1646. continue;
  1647. if (!sb_has_quota_loaded(sb, cnt))
  1648. continue;
  1649. if (flags & DQUOT_SUSPENDED) {
  1650. spin_lock(&dq_state_lock);
  1651. dqopt->flags |=
  1652. dquot_state_flag(DQUOT_SUSPENDED, cnt);
  1653. spin_unlock(&dq_state_lock);
  1654. } else {
  1655. spin_lock(&dq_state_lock);
  1656. dqopt->flags &= ~dquot_state_flag(flags, cnt);
  1657. /* Turning off suspended quotas? */
  1658. if (!sb_has_quota_loaded(sb, cnt) &&
  1659. sb_has_quota_suspended(sb, cnt)) {
  1660. dqopt->flags &= ~dquot_state_flag(
  1661. DQUOT_SUSPENDED, cnt);
  1662. spin_unlock(&dq_state_lock);
  1663. iput(dqopt->files[cnt]);
  1664. dqopt->files[cnt] = NULL;
  1665. continue;
  1666. }
  1667. spin_unlock(&dq_state_lock);
  1668. }
  1669. /* We still have to keep quota loaded? */
  1670. if (sb_has_quota_loaded(sb, cnt) && !(flags & DQUOT_SUSPENDED))
  1671. continue;
  1672. /* Note: these are blocking operations */
  1673. drop_dquot_ref(sb, cnt);
  1674. invalidate_dquots(sb, cnt);
  1675. /*
  1676. * Now all dquots should be invalidated, all writes done so we should be only
  1677. * users of the info. No locks needed.
  1678. */
  1679. if (info_dirty(&dqopt->info[cnt]))
  1680. sb->dq_op->write_info(sb, cnt);
  1681. if (dqopt->ops[cnt]->free_file_info)
  1682. dqopt->ops[cnt]->free_file_info(sb, cnt);
  1683. put_quota_format(dqopt->info[cnt].dqi_format);
  1684. toputinode[cnt] = dqopt->files[cnt];
  1685. if (!sb_has_quota_loaded(sb, cnt))
  1686. dqopt->files[cnt] = NULL;
  1687. dqopt->info[cnt].dqi_flags = 0;
  1688. dqopt->info[cnt].dqi_igrace = 0;
  1689. dqopt->info[cnt].dqi_bgrace = 0;
  1690. dqopt->ops[cnt] = NULL;
  1691. }
  1692. mutex_unlock(&dqopt->dqonoff_mutex);
  1693. /* Skip syncing and setting flags if quota files are hidden */
  1694. if (dqopt->flags & DQUOT_QUOTA_SYS_FILE)
  1695. goto put_inodes;
  1696. /* Sync the superblock so that buffers with quota data are written to
  1697. * disk (and so userspace sees correct data afterwards). */
  1698. if (sb->s_op->sync_fs)
  1699. sb->s_op->sync_fs(sb, 1);
  1700. sync_blockdev(sb->s_bdev);
  1701. /* Now the quota files are just ordinary files and we can set the
  1702. * inode flags back. Moreover we discard the pagecache so that
  1703. * userspace sees the writes we did bypassing the pagecache. We
  1704. * must also discard the blockdev buffers so that we see the
  1705. * changes done by userspace on the next quotaon() */
  1706. for (cnt = 0; cnt < MAXQUOTAS; cnt++)
  1707. if (toputinode[cnt]) {
  1708. mutex_lock(&dqopt->dqonoff_mutex);
  1709. /* If quota was reenabled in the meantime, we have
  1710. * nothing to do */
  1711. if (!sb_has_quota_loaded(sb, cnt)) {
  1712. mutex_lock_nested(&toputinode[cnt]->i_mutex, I_MUTEX_QUOTA);
  1713. toputinode[cnt]->i_flags &= ~(S_IMMUTABLE |
  1714. S_NOATIME | S_NOQUOTA);
  1715. truncate_inode_pages(&toputinode[cnt]->i_data, 0);
  1716. mutex_unlock(&toputinode[cnt]->i_mutex);
  1717. mark_inode_dirty(toputinode[cnt]);
  1718. }
  1719. mutex_unlock(&dqopt->dqonoff_mutex);
  1720. }
  1721. if (sb->s_bdev)
  1722. invalidate_bdev(sb->s_bdev);
  1723. put_inodes:
  1724. for (cnt = 0; cnt < MAXQUOTAS; cnt++)
  1725. if (toputinode[cnt]) {
  1726. /* On remount RO, we keep the inode pointer so that we
  1727. * can reenable quota on the subsequent remount RW. We
  1728. * have to check 'flags' variable and not use sb_has_
  1729. * function because another quotaon / quotaoff could
  1730. * change global state before we got here. We refuse
  1731. * to suspend quotas when there is pending delete on
  1732. * the quota file... */
  1733. if (!(flags & DQUOT_SUSPENDED))
  1734. iput(toputinode[cnt]);
  1735. else if (!toputinode[cnt]->i_nlink)
  1736. ret = -EBUSY;
  1737. }
  1738. return ret;
  1739. }
  1740. int vfs_quota_off(struct super_block *sb, int type, int remount)
  1741. {
  1742. return vfs_quota_disable(sb, type, remount ? DQUOT_SUSPENDED :
  1743. (DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED));
  1744. }
  1745. /*
  1746. * Turn quotas on on a device
  1747. */
  1748. /*
  1749. * Helper function to turn quotas on when we already have the inode of
  1750. * quota file and no quota information is loaded.
  1751. */
  1752. static int vfs_load_quota_inode(struct inode *inode, int type, int format_id,
  1753. unsigned int flags)
  1754. {
  1755. struct quota_format_type *fmt = find_quota_format(format_id);
  1756. struct super_block *sb = inode->i_sb;
  1757. struct quota_info *dqopt = sb_dqopt(sb);
  1758. int error;
  1759. int oldflags = -1;
  1760. if (!fmt)
  1761. return -ESRCH;
  1762. if (!S_ISREG(inode->i_mode)) {
  1763. error = -EACCES;
  1764. goto out_fmt;
  1765. }
  1766. if (IS_RDONLY(inode)) {
  1767. error = -EROFS;
  1768. goto out_fmt;
  1769. }
  1770. if (!sb->s_op->quota_write || !sb->s_op->quota_read) {
  1771. error = -EINVAL;
  1772. goto out_fmt;
  1773. }
  1774. /* Usage always has to be set... */
  1775. if (!(flags & DQUOT_USAGE_ENABLED)) {
  1776. error = -EINVAL;
  1777. goto out_fmt;
  1778. }
  1779. if (!(dqopt->flags & DQUOT_QUOTA_SYS_FILE)) {
  1780. /* As we bypass the pagecache we must now flush the inode so
  1781. * that we see all the changes from userspace... */
  1782. write_inode_now(inode, 1);
  1783. /* And now flush the block cache so that kernel sees the
  1784. * changes */
  1785. invalidate_bdev(sb->s_bdev);
  1786. }
  1787. mutex_lock(&inode->i_mutex);
  1788. mutex_lock(&dqopt->dqonoff_mutex);
  1789. if (sb_has_quota_loaded(sb, type)) {
  1790. error = -EBUSY;
  1791. goto out_lock;
  1792. }
  1793. if (!(dqopt->flags & DQUOT_QUOTA_SYS_FILE)) {
  1794. /* We don't want quota and atime on quota files (deadlocks
  1795. * possible) Also nobody should write to the file - we use
  1796. * special IO operations which ignore the immutable bit. */
  1797. down_write(&dqopt->dqptr_sem);
  1798. oldflags = inode->i_flags & (S_NOATIME | S_IMMUTABLE | S_NOQUOTA);
  1799. inode->i_flags |= S_NOQUOTA | S_NOATIME | S_IMMUTABLE;
  1800. up_write(&dqopt->dqptr_sem);
  1801. sb->dq_op->drop(inode);
  1802. }
  1803. error = -EIO;
  1804. dqopt->files[type] = igrab(inode);
  1805. if (!dqopt->files[type])
  1806. goto out_lock;
  1807. error = -EINVAL;
  1808. if (!fmt->qf_ops->check_quota_file(sb, type))
  1809. goto out_file_init;
  1810. dqopt->ops[type] = fmt->qf_ops;
  1811. dqopt->info[type].dqi_format = fmt;
  1812. dqopt->info[type].dqi_fmt_id = format_id;
  1813. INIT_LIST_HEAD(&dqopt->info[type].dqi_dirty_list);
  1814. mutex_lock(&dqopt->dqio_mutex);
  1815. if ((error = dqopt->ops[type]->read_file_info(sb, type)) < 0) {
  1816. mutex_unlock(&dqopt->dqio_mutex);
  1817. goto out_file_init;
  1818. }
  1819. mutex_unlock(&dqopt->dqio_mutex);
  1820. mutex_unlock(&inode->i_mutex);
  1821. spin_lock(&dq_state_lock);
  1822. dqopt->flags |= dquot_state_flag(flags, type);
  1823. spin_unlock(&dq_state_lock);
  1824. add_dquot_ref(sb, type);
  1825. mutex_unlock(&dqopt->dqonoff_mutex);
  1826. return 0;
  1827. out_file_init:
  1828. dqopt->files[type] = NULL;
  1829. iput(inode);
  1830. out_lock:
  1831. mutex_unlock(&dqopt->dqonoff_mutex);
  1832. if (oldflags != -1) {
  1833. down_write(&dqopt->dqptr_sem);
  1834. /* Set the flags back (in the case of accidental quotaon()
  1835. * on a wrong file we don't want to mess up the flags) */
  1836. inode->i_flags &= ~(S_NOATIME | S_NOQUOTA | S_IMMUTABLE);
  1837. inode->i_flags |= oldflags;
  1838. up_write(&dqopt->dqptr_sem);
  1839. }
  1840. mutex_unlock(&inode->i_mutex);
  1841. out_fmt:
  1842. put_quota_format(fmt);
  1843. return error;
  1844. }
  1845. /* Reenable quotas on remount RW */
  1846. static int vfs_quota_on_remount(struct super_block *sb, int type)
  1847. {
  1848. struct quota_info *dqopt = sb_dqopt(sb);
  1849. struct inode *inode;
  1850. int ret;
  1851. unsigned int flags;
  1852. mutex_lock(&dqopt->dqonoff_mutex);
  1853. if (!sb_has_quota_suspended(sb, type)) {
  1854. mutex_unlock(&dqopt->dqonoff_mutex);
  1855. return 0;
  1856. }
  1857. inode = dqopt->files[type];
  1858. dqopt->files[type] = NULL;
  1859. spin_lock(&dq_state_lock);
  1860. flags = dqopt->flags & dquot_state_flag(DQUOT_USAGE_ENABLED |
  1861. DQUOT_LIMITS_ENABLED, type);
  1862. dqopt->flags &= ~dquot_state_flag(DQUOT_STATE_FLAGS, type);
  1863. spin_unlock(&dq_state_lock);
  1864. mutex_unlock(&dqopt->dqonoff_mutex);
  1865. flags = dquot_generic_flag(flags, type);
  1866. ret = vfs_load_quota_inode(inode, type, dqopt->info[type].dqi_fmt_id,
  1867. flags);
  1868. iput(inode);
  1869. return ret;
  1870. }
  1871. int vfs_quota_on_path(struct super_block *sb, int type, int format_id,
  1872. struct path *path)
  1873. {
  1874. int error = security_quota_on(path->dentry);
  1875. if (error)
  1876. return error;
  1877. /* Quota file not on the same filesystem? */
  1878. if (path->mnt->mnt_sb != sb)
  1879. error = -EXDEV;
  1880. else
  1881. error = vfs_load_quota_inode(path->dentry->d_inode, type,
  1882. format_id, DQUOT_USAGE_ENABLED |
  1883. DQUOT_LIMITS_ENABLED);
  1884. return error;
  1885. }
  1886. int vfs_quota_on(struct super_block *sb, int type, int format_id, char *name,
  1887. int remount)
  1888. {
  1889. struct path path;
  1890. int error;
  1891. if (remount)
  1892. return vfs_quota_on_remount(sb, type);
  1893. error = kern_path(name, LOOKUP_FOLLOW, &path);
  1894. if (!error) {
  1895. error = vfs_quota_on_path(sb, type, format_id, &path);
  1896. path_put(&path);
  1897. }
  1898. return error;
  1899. }
  1900. /*
  1901. * More powerful function for turning on quotas allowing setting
  1902. * of individual quota flags
  1903. */
  1904. int vfs_quota_enable(struct inode *inode, int type, int format_id,
  1905. unsigned int flags)
  1906. {
  1907. int ret = 0;
  1908. struct super_block *sb = inode->i_sb;
  1909. struct quota_info *dqopt = sb_dqopt(sb);
  1910. /* Just unsuspend quotas? */
  1911. if (flags & DQUOT_SUSPENDED)
  1912. return vfs_quota_on_remount(sb, type);
  1913. if (!flags)
  1914. return 0;
  1915. /* Just updating flags needed? */
  1916. if (sb_has_quota_loaded(sb, type)) {
  1917. mutex_lock(&dqopt->dqonoff_mutex);
  1918. /* Now do a reliable test... */
  1919. if (!sb_has_quota_loaded(sb, type)) {
  1920. mutex_unlock(&dqopt->dqonoff_mutex);
  1921. goto load_quota;
  1922. }
  1923. if (flags & DQUOT_USAGE_ENABLED &&
  1924. sb_has_quota_usage_enabled(sb, type)) {
  1925. ret = -EBUSY;
  1926. goto out_lock;
  1927. }
  1928. if (flags & DQUOT_LIMITS_ENABLED &&
  1929. sb_has_quota_limits_enabled(sb, type)) {
  1930. ret = -EBUSY;
  1931. goto out_lock;
  1932. }
  1933. spin_lock(&dq_state_lock);
  1934. sb_dqopt(sb)->flags |= dquot_state_flag(flags, type);
  1935. spin_unlock(&dq_state_lock);
  1936. out_lock:
  1937. mutex_unlock(&dqopt->dqonoff_mutex);
  1938. return ret;
  1939. }
  1940. load_quota:
  1941. return vfs_load_quota_inode(inode, type, format_id, flags);
  1942. }
  1943. /*
  1944. * This function is used when filesystem needs to initialize quotas
  1945. * during mount time.
  1946. */
  1947. int vfs_quota_on_mount(struct super_block *sb, char *qf_name,
  1948. int format_id, int type)
  1949. {
  1950. struct dentry *dentry;
  1951. int error;
  1952. dentry = lookup_one_len(qf_name, sb->s_root, strlen(qf_name));
  1953. if (IS_ERR(dentry))
  1954. return PTR_ERR(dentry);
  1955. if (!dentry->d_inode) {
  1956. error = -ENOENT;
  1957. goto out;
  1958. }
  1959. error = security_quota_on(dentry);
  1960. if (!error)
  1961. error = vfs_load_quota_inode(dentry->d_inode, type, format_id,
  1962. DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED);
  1963. out:
  1964. dput(dentry);
  1965. return error;
  1966. }
  1967. /* Wrapper to turn on quotas when remounting rw */
  1968. int vfs_dq_quota_on_remount(struct super_block *sb)
  1969. {
  1970. int cnt;
  1971. int ret = 0, err;
  1972. if (!sb->s_qcop || !sb->s_qcop->quota_on)
  1973. return -ENOSYS;
  1974. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1975. err = sb->s_qcop->quota_on(sb, cnt, 0, NULL, 1);
  1976. if (err < 0 && !ret)
  1977. ret = err;
  1978. }
  1979. return ret;
  1980. }
  1981. static inline qsize_t qbtos(qsize_t blocks)
  1982. {
  1983. return blocks << QIF_DQBLKSIZE_BITS;
  1984. }
  1985. static inline qsize_t stoqb(qsize_t space)
  1986. {
  1987. return (space + QIF_DQBLKSIZE - 1) >> QIF_DQBLKSIZE_BITS;
  1988. }
  1989. /* Generic routine for getting common part of quota structure */
  1990. static void do_get_dqblk(struct dquot *dquot, struct if_dqblk *di)
  1991. {
  1992. struct mem_dqblk *dm = &dquot->dq_dqb;
  1993. spin_lock(&dq_data_lock);
  1994. di->dqb_bhardlimit = stoqb(dm->dqb_bhardlimit);
  1995. di->dqb_bsoftlimit = stoqb(dm->dqb_bsoftlimit);
  1996. di->dqb_curspace = dm->dqb_curspace + dm->dqb_rsvspace;
  1997. di->dqb_ihardlimit = dm->dqb_ihardlimit;
  1998. di->dqb_isoftlimit = dm->dqb_isoftlimit;
  1999. di->dqb_curinodes = dm->dqb_curinodes;
  2000. di->dqb_btime = dm->dqb_btime;
  2001. di->dqb_itime = dm->dqb_itime;
  2002. di->dqb_valid = QIF_ALL;
  2003. spin_unlock(&dq_data_lock);
  2004. }
  2005. int vfs_get_dqblk(struct super_block *sb, int type, qid_t id, struct if_dqblk *di)
  2006. {
  2007. struct dquot *dquot;
  2008. dquot = dqget(sb, id, type);
  2009. if (dquot == NODQUOT)
  2010. return -ESRCH;
  2011. do_get_dqblk(dquot, di);
  2012. dqput(dquot);
  2013. return 0;
  2014. }
  2015. /* Generic routine for setting common part of quota structure */
  2016. static int do_set_dqblk(struct dquot *dquot, struct if_dqblk *di)
  2017. {
  2018. struct mem_dqblk *dm = &dquot->dq_dqb;
  2019. int check_blim = 0, check_ilim = 0;
  2020. struct mem_dqinfo *dqi = &sb_dqopt(dquot->dq_sb)->info[dquot->dq_type];
  2021. if ((di->dqb_valid & QIF_BLIMITS &&
  2022. (di->dqb_bhardlimit > dqi->dqi_maxblimit ||
  2023. di->dqb_bsoftlimit > dqi->dqi_maxblimit)) ||
  2024. (di->dqb_valid & QIF_ILIMITS &&
  2025. (di->dqb_ihardlimit > dqi->dqi_maxilimit ||
  2026. di->dqb_isoftlimit > dqi->dqi_maxilimit)))
  2027. return -ERANGE;
  2028. spin_lock(&dq_data_lock);
  2029. if (di->dqb_valid & QIF_SPACE) {
  2030. dm->dqb_curspace = di->dqb_curspace - dm->dqb_rsvspace;
  2031. check_blim = 1;
  2032. __set_bit(DQ_LASTSET_B + QIF_SPACE_B, &dquot->dq_flags);
  2033. }
  2034. if (di->dqb_valid & QIF_BLIMITS) {
  2035. dm->dqb_bsoftlimit = qbtos(di->dqb_bsoftlimit);
  2036. dm->dqb_bhardlimit = qbtos(di->dqb_bhardlimit);
  2037. check_blim = 1;
  2038. __set_bit(DQ_LASTSET_B + QIF_BLIMITS_B, &dquot->dq_flags);
  2039. }
  2040. if (di->dqb_valid & QIF_INODES) {
  2041. dm->dqb_curinodes = di->dqb_curinodes;
  2042. check_ilim = 1;
  2043. __set_bit(DQ_LASTSET_B + QIF_INODES_B, &dquot->dq_flags);
  2044. }
  2045. if (di->dqb_valid & QIF_ILIMITS) {
  2046. dm->dqb_isoftlimit = di->dqb_isoftlimit;
  2047. dm->dqb_ihardlimit = di->dqb_ihardlimit;
  2048. check_ilim = 1;
  2049. __set_bit(DQ_LASTSET_B + QIF_ILIMITS_B, &dquot->dq_flags);
  2050. }
  2051. if (di->dqb_valid & QIF_BTIME) {
  2052. dm->dqb_btime = di->dqb_btime;
  2053. check_blim = 1;
  2054. __set_bit(DQ_LASTSET_B + QIF_BTIME_B, &dquot->dq_flags);
  2055. }
  2056. if (di->dqb_valid & QIF_ITIME) {
  2057. dm->dqb_itime = di->dqb_itime;
  2058. check_ilim = 1;
  2059. __set_bit(DQ_LASTSET_B + QIF_ITIME_B, &dquot->dq_flags);
  2060. }
  2061. if (check_blim) {
  2062. if (!dm->dqb_bsoftlimit || dm->dqb_curspace < dm->dqb_bsoftlimit) {
  2063. dm->dqb_btime = 0;
  2064. clear_bit(DQ_BLKS_B, &dquot->dq_flags);
  2065. }
  2066. else if (!(di->dqb_valid & QIF_BTIME)) /* Set grace only if user hasn't provided his own... */
  2067. dm->dqb_btime = get_seconds() + dqi->dqi_bgrace;
  2068. }
  2069. if (check_ilim) {
  2070. if (!dm->dqb_isoftlimit || dm->dqb_curinodes < dm->dqb_isoftlimit) {
  2071. dm->dqb_itime = 0;
  2072. clear_bit(DQ_INODES_B, &dquot->dq_flags);
  2073. }
  2074. else if (!(di->dqb_valid & QIF_ITIME)) /* Set grace only if user hasn't provided his own... */
  2075. dm->dqb_itime = get_seconds() + dqi->dqi_igrace;
  2076. }
  2077. if (dm->dqb_bhardlimit || dm->dqb_bsoftlimit || dm->dqb_ihardlimit || dm->dqb_isoftlimit)
  2078. clear_bit(DQ_FAKE_B, &dquot->dq_flags);
  2079. else
  2080. set_bit(DQ_FAKE_B, &dquot->dq_flags);
  2081. spin_unlock(&dq_data_lock);
  2082. mark_dquot_dirty(dquot);
  2083. return 0;
  2084. }
  2085. int vfs_set_dqblk(struct super_block *sb, int type, qid_t id, struct if_dqblk *di)
  2086. {
  2087. struct dquot *dquot;
  2088. int rc;
  2089. dquot = dqget(sb, id, type);
  2090. if (!dquot) {
  2091. rc = -ESRCH;
  2092. goto out;
  2093. }
  2094. rc = do_set_dqblk(dquot, di);
  2095. dqput(dquot);
  2096. out:
  2097. return rc;
  2098. }
  2099. /* Generic routine for getting common part of quota file information */
  2100. int vfs_get_dqinfo(struct super_block *sb, int type, struct if_dqinfo *ii)
  2101. {
  2102. struct mem_dqinfo *mi;
  2103. mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
  2104. if (!sb_has_quota_active(sb, type)) {
  2105. mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
  2106. return -ESRCH;
  2107. }
  2108. mi = sb_dqopt(sb)->info + type;
  2109. spin_lock(&dq_data_lock);
  2110. ii->dqi_bgrace = mi->dqi_bgrace;
  2111. ii->dqi_igrace = mi->dqi_igrace;
  2112. ii->dqi_flags = mi->dqi_flags & DQF_MASK;
  2113. ii->dqi_valid = IIF_ALL;
  2114. spin_unlock(&dq_data_lock);
  2115. mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
  2116. return 0;
  2117. }
  2118. /* Generic routine for setting common part of quota file information */
  2119. int vfs_set_dqinfo(struct super_block *sb, int type, struct if_dqinfo *ii)
  2120. {
  2121. struct mem_dqinfo *mi;
  2122. int err = 0;
  2123. mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
  2124. if (!sb_has_quota_active(sb, type)) {
  2125. err = -ESRCH;
  2126. goto out;
  2127. }
  2128. mi = sb_dqopt(sb)->info + type;
  2129. spin_lock(&dq_data_lock);
  2130. if (ii->dqi_valid & IIF_BGRACE)
  2131. mi->dqi_bgrace = ii->dqi_bgrace;
  2132. if (ii->dqi_valid & IIF_IGRACE)
  2133. mi->dqi_igrace = ii->dqi_igrace;
  2134. if (ii->dqi_valid & IIF_FLAGS)
  2135. mi->dqi_flags = (mi->dqi_flags & ~DQF_MASK) | (ii->dqi_flags & DQF_MASK);
  2136. spin_unlock(&dq_data_lock);
  2137. mark_info_dirty(sb, type);
  2138. /* Force write to disk */
  2139. sb->dq_op->write_info(sb, type);
  2140. out:
  2141. mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
  2142. return err;
  2143. }
  2144. struct quotactl_ops vfs_quotactl_ops = {
  2145. .quota_on = vfs_quota_on,
  2146. .quota_off = vfs_quota_off,
  2147. .quota_sync = vfs_quota_sync,
  2148. .get_info = vfs_get_dqinfo,
  2149. .set_info = vfs_set_dqinfo,
  2150. .get_dqblk = vfs_get_dqblk,
  2151. .set_dqblk = vfs_set_dqblk
  2152. };
  2153. static ctl_table fs_dqstats_table[] = {
  2154. {
  2155. .ctl_name = FS_DQ_LOOKUPS,
  2156. .procname = "lookups",
  2157. .data = &dqstats.lookups,
  2158. .maxlen = sizeof(int),
  2159. .mode = 0444,
  2160. .proc_handler = &proc_dointvec,
  2161. },
  2162. {
  2163. .ctl_name = FS_DQ_DROPS,
  2164. .procname = "drops",
  2165. .data = &dqstats.drops,
  2166. .maxlen = sizeof(int),
  2167. .mode = 0444,
  2168. .proc_handler = &proc_dointvec,
  2169. },
  2170. {
  2171. .ctl_name = FS_DQ_READS,
  2172. .procname = "reads",
  2173. .data = &dqstats.reads,
  2174. .maxlen = sizeof(int),
  2175. .mode = 0444,
  2176. .proc_handler = &proc_dointvec,
  2177. },
  2178. {
  2179. .ctl_name = FS_DQ_WRITES,
  2180. .procname = "writes",
  2181. .data = &dqstats.writes,
  2182. .maxlen = sizeof(int),
  2183. .mode = 0444,
  2184. .proc_handler = &proc_dointvec,
  2185. },
  2186. {
  2187. .ctl_name = FS_DQ_CACHE_HITS,
  2188. .procname = "cache_hits",
  2189. .data = &dqstats.cache_hits,
  2190. .maxlen = sizeof(int),
  2191. .mode = 0444,
  2192. .proc_handler = &proc_dointvec,
  2193. },
  2194. {
  2195. .ctl_name = FS_DQ_ALLOCATED,
  2196. .procname = "allocated_dquots",
  2197. .data = &dqstats.allocated_dquots,
  2198. .maxlen = sizeof(int),
  2199. .mode = 0444,
  2200. .proc_handler = &proc_dointvec,
  2201. },
  2202. {
  2203. .ctl_name = FS_DQ_FREE,
  2204. .procname = "free_dquots",
  2205. .data = &dqstats.free_dquots,
  2206. .maxlen = sizeof(int),
  2207. .mode = 0444,
  2208. .proc_handler = &proc_dointvec,
  2209. },
  2210. {
  2211. .ctl_name = FS_DQ_SYNCS,
  2212. .procname = "syncs",
  2213. .data = &dqstats.syncs,
  2214. .maxlen = sizeof(int),
  2215. .mode = 0444,
  2216. .proc_handler = &proc_dointvec,
  2217. },
  2218. #ifdef CONFIG_PRINT_QUOTA_WARNING
  2219. {
  2220. .ctl_name = FS_DQ_WARNINGS,
  2221. .procname = "warnings",
  2222. .data = &flag_print_warnings,
  2223. .maxlen = sizeof(int),
  2224. .mode = 0644,
  2225. .proc_handler = &proc_dointvec,
  2226. },
  2227. #endif
  2228. { .ctl_name = 0 },
  2229. };
  2230. static ctl_table fs_table[] = {
  2231. {
  2232. .ctl_name = FS_DQSTATS,
  2233. .procname = "quota",
  2234. .mode = 0555,
  2235. .child = fs_dqstats_table,
  2236. },
  2237. { .ctl_name = 0 },
  2238. };
  2239. static ctl_table sys_table[] = {
  2240. {
  2241. .ctl_name = CTL_FS,
  2242. .procname = "fs",
  2243. .mode = 0555,
  2244. .child = fs_table,
  2245. },
  2246. { .ctl_name = 0 },
  2247. };
  2248. static int __init dquot_init(void)
  2249. {
  2250. int i;
  2251. unsigned long nr_hash, order;
  2252. printk(KERN_NOTICE "VFS: Disk quotas %s\n", __DQUOT_VERSION__);
  2253. register_sysctl_table(sys_table);
  2254. dquot_cachep = kmem_cache_create("dquot",
  2255. sizeof(struct dquot), sizeof(unsigned long) * 4,
  2256. (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
  2257. SLAB_MEM_SPREAD|SLAB_PANIC),
  2258. NULL);
  2259. order = 0;
  2260. dquot_hash = (struct hlist_head *)__get_free_pages(GFP_ATOMIC, order);
  2261. if (!dquot_hash)
  2262. panic("Cannot create dquot hash table");
  2263. /* Find power-of-two hlist_heads which can fit into allocation */
  2264. nr_hash = (1UL << order) * PAGE_SIZE / sizeof(struct hlist_head);
  2265. dq_hash_bits = 0;
  2266. do {
  2267. dq_hash_bits++;
  2268. } while (nr_hash >> dq_hash_bits);
  2269. dq_hash_bits--;
  2270. nr_hash = 1UL << dq_hash_bits;
  2271. dq_hash_mask = nr_hash - 1;
  2272. for (i = 0; i < nr_hash; i++)
  2273. INIT_HLIST_HEAD(dquot_hash + i);
  2274. printk("Dquot-cache hash table entries: %ld (order %ld, %ld bytes)\n",
  2275. nr_hash, order, (PAGE_SIZE << order));
  2276. register_shrinker(&dqcache_shrinker);
  2277. #ifdef CONFIG_QUOTA_NETLINK_INTERFACE
  2278. if (genl_register_family(&quota_genl_family) != 0)
  2279. printk(KERN_ERR "VFS: Failed to create quota netlink interface.\n");
  2280. #endif
  2281. return 0;
  2282. }
  2283. module_init(dquot_init);
  2284. EXPORT_SYMBOL(register_quota_format);
  2285. EXPORT_SYMBOL(unregister_quota_format);
  2286. EXPORT_SYMBOL(dqstats);
  2287. EXPORT_SYMBOL(dq_data_lock);
  2288. EXPORT_SYMBOL(vfs_quota_enable);
  2289. EXPORT_SYMBOL(vfs_quota_on);
  2290. EXPORT_SYMBOL(vfs_quota_on_path);
  2291. EXPORT_SYMBOL(vfs_quota_on_mount);
  2292. EXPORT_SYMBOL(vfs_quota_disable);
  2293. EXPORT_SYMBOL(vfs_quota_off);
  2294. EXPORT_SYMBOL(dquot_scan_active);
  2295. EXPORT_SYMBOL(vfs_quota_sync);
  2296. EXPORT_SYMBOL(vfs_get_dqinfo);
  2297. EXPORT_SYMBOL(vfs_set_dqinfo);
  2298. EXPORT_SYMBOL(vfs_get_dqblk);
  2299. EXPORT_SYMBOL(vfs_set_dqblk);
  2300. EXPORT_SYMBOL(dquot_commit);
  2301. EXPORT_SYMBOL(dquot_commit_info);
  2302. EXPORT_SYMBOL(dquot_acquire);
  2303. EXPORT_SYMBOL(dquot_release);
  2304. EXPORT_SYMBOL(dquot_mark_dquot_dirty);
  2305. EXPORT_SYMBOL(dquot_initialize);
  2306. EXPORT_SYMBOL(dquot_drop);
  2307. EXPORT_SYMBOL(vfs_dq_drop);
  2308. EXPORT_SYMBOL(dqget);
  2309. EXPORT_SYMBOL(dqput);
  2310. EXPORT_SYMBOL(dquot_alloc_space);
  2311. EXPORT_SYMBOL(dquot_alloc_inode);
  2312. EXPORT_SYMBOL(dquot_free_space);
  2313. EXPORT_SYMBOL(dquot_free_inode);
  2314. EXPORT_SYMBOL(dquot_transfer);
  2315. EXPORT_SYMBOL(vfs_dq_transfer);
  2316. EXPORT_SYMBOL(vfs_dq_quota_on_remount);