quota_v2.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713
  1. /*
  2. * vfsv0 quota IO operations on file
  3. */
  4. #include <linux/errno.h>
  5. #include <linux/fs.h>
  6. #include <linux/mount.h>
  7. #include <linux/dqblk_v2.h>
  8. #include <linux/kernel.h>
  9. #include <linux/init.h>
  10. #include <linux/module.h>
  11. #include <linux/slab.h>
  12. #include <linux/quotaops.h>
  13. #include <asm/byteorder.h>
  14. #include "quotaio_v2.h"
  15. MODULE_AUTHOR("Jan Kara");
  16. MODULE_DESCRIPTION("Quota format v2 support");
  17. MODULE_LICENSE("GPL");
  18. #define __QUOTA_V2_PARANOIA
  19. typedef char *dqbuf_t;
  20. #define GETIDINDEX(id, depth) (((id) >> ((V2_DQTREEDEPTH-(depth)-1)*8)) & 0xff)
  21. #define GETENTRIES(buf) ((struct v2_disk_dqblk *)(((char *)buf)+sizeof(struct v2_disk_dqdbheader)))
  22. #define QUOTABLOCK_BITS 10
  23. #define QUOTABLOCK_SIZE (1 << QUOTABLOCK_BITS)
  24. static inline qsize_t v2_stoqb(qsize_t space)
  25. {
  26. return (space + QUOTABLOCK_SIZE - 1) >> QUOTABLOCK_BITS;
  27. }
  28. static inline qsize_t v2_qbtos(qsize_t blocks)
  29. {
  30. return blocks << QUOTABLOCK_BITS;
  31. }
  32. /* Check whether given file is really vfsv0 quotafile */
  33. static int v2_check_quota_file(struct super_block *sb, int type)
  34. {
  35. struct v2_disk_dqheader dqhead;
  36. ssize_t size;
  37. static const uint quota_magics[] = V2_INITQMAGICS;
  38. static const uint quota_versions[] = V2_INITQVERSIONS;
  39. size = sb->s_op->quota_read(sb, type, (char *)&dqhead, sizeof(struct v2_disk_dqheader), 0);
  40. if (size != sizeof(struct v2_disk_dqheader)) {
  41. printk("quota_v2: failed read expected=%zd got=%zd\n",
  42. sizeof(struct v2_disk_dqheader), size);
  43. return 0;
  44. }
  45. if (le32_to_cpu(dqhead.dqh_magic) != quota_magics[type] ||
  46. le32_to_cpu(dqhead.dqh_version) != quota_versions[type])
  47. return 0;
  48. return 1;
  49. }
  50. /* Read information header from quota file */
  51. static int v2_read_file_info(struct super_block *sb, int type)
  52. {
  53. struct v2_disk_dqinfo dinfo;
  54. struct mem_dqinfo *info = sb_dqopt(sb)->info+type;
  55. ssize_t size;
  56. size = sb->s_op->quota_read(sb, type, (char *)&dinfo,
  57. sizeof(struct v2_disk_dqinfo), V2_DQINFOOFF);
  58. if (size != sizeof(struct v2_disk_dqinfo)) {
  59. printk(KERN_WARNING "Can't read info structure on device %s.\n",
  60. sb->s_id);
  61. return -1;
  62. }
  63. /* limits are stored as unsigned 32-bit data */
  64. info->dqi_maxblimit = 0xffffffff;
  65. info->dqi_maxilimit = 0xffffffff;
  66. info->dqi_bgrace = le32_to_cpu(dinfo.dqi_bgrace);
  67. info->dqi_igrace = le32_to_cpu(dinfo.dqi_igrace);
  68. info->dqi_flags = le32_to_cpu(dinfo.dqi_flags);
  69. info->u.v2_i.dqi_blocks = le32_to_cpu(dinfo.dqi_blocks);
  70. info->u.v2_i.dqi_free_blk = le32_to_cpu(dinfo.dqi_free_blk);
  71. info->u.v2_i.dqi_free_entry = le32_to_cpu(dinfo.dqi_free_entry);
  72. return 0;
  73. }
  74. /* Write information header to quota file */
  75. static int v2_write_file_info(struct super_block *sb, int type)
  76. {
  77. struct v2_disk_dqinfo dinfo;
  78. struct mem_dqinfo *info = sb_dqopt(sb)->info+type;
  79. ssize_t size;
  80. spin_lock(&dq_data_lock);
  81. info->dqi_flags &= ~DQF_INFO_DIRTY;
  82. dinfo.dqi_bgrace = cpu_to_le32(info->dqi_bgrace);
  83. dinfo.dqi_igrace = cpu_to_le32(info->dqi_igrace);
  84. dinfo.dqi_flags = cpu_to_le32(info->dqi_flags & DQF_MASK);
  85. spin_unlock(&dq_data_lock);
  86. dinfo.dqi_blocks = cpu_to_le32(info->u.v2_i.dqi_blocks);
  87. dinfo.dqi_free_blk = cpu_to_le32(info->u.v2_i.dqi_free_blk);
  88. dinfo.dqi_free_entry = cpu_to_le32(info->u.v2_i.dqi_free_entry);
  89. size = sb->s_op->quota_write(sb, type, (char *)&dinfo,
  90. sizeof(struct v2_disk_dqinfo), V2_DQINFOOFF);
  91. if (size != sizeof(struct v2_disk_dqinfo)) {
  92. printk(KERN_WARNING "Can't write info structure on device %s.\n",
  93. sb->s_id);
  94. return -1;
  95. }
  96. return 0;
  97. }
  98. static void disk2memdqb(struct mem_dqblk *m, struct v2_disk_dqblk *d)
  99. {
  100. m->dqb_ihardlimit = le32_to_cpu(d->dqb_ihardlimit);
  101. m->dqb_isoftlimit = le32_to_cpu(d->dqb_isoftlimit);
  102. m->dqb_curinodes = le32_to_cpu(d->dqb_curinodes);
  103. m->dqb_itime = le64_to_cpu(d->dqb_itime);
  104. m->dqb_bhardlimit = v2_qbtos(le32_to_cpu(d->dqb_bhardlimit));
  105. m->dqb_bsoftlimit = v2_qbtos(le32_to_cpu(d->dqb_bsoftlimit));
  106. m->dqb_curspace = le64_to_cpu(d->dqb_curspace);
  107. m->dqb_btime = le64_to_cpu(d->dqb_btime);
  108. }
  109. static void mem2diskdqb(struct v2_disk_dqblk *d, struct mem_dqblk *m, qid_t id)
  110. {
  111. d->dqb_ihardlimit = cpu_to_le32(m->dqb_ihardlimit);
  112. d->dqb_isoftlimit = cpu_to_le32(m->dqb_isoftlimit);
  113. d->dqb_curinodes = cpu_to_le32(m->dqb_curinodes);
  114. d->dqb_itime = cpu_to_le64(m->dqb_itime);
  115. d->dqb_bhardlimit = cpu_to_le32(v2_stoqb(m->dqb_bhardlimit));
  116. d->dqb_bsoftlimit = cpu_to_le32(v2_stoqb(m->dqb_bsoftlimit));
  117. d->dqb_curspace = cpu_to_le64(m->dqb_curspace);
  118. d->dqb_btime = cpu_to_le64(m->dqb_btime);
  119. d->dqb_id = cpu_to_le32(id);
  120. }
  121. static dqbuf_t getdqbuf(void)
  122. {
  123. dqbuf_t buf = kmalloc(V2_DQBLKSIZE, GFP_NOFS);
  124. if (!buf)
  125. printk(KERN_WARNING "VFS: Not enough memory for quota buffers.\n");
  126. return buf;
  127. }
  128. static inline void freedqbuf(dqbuf_t buf)
  129. {
  130. kfree(buf);
  131. }
  132. static inline ssize_t read_blk(struct super_block *sb, int type, uint blk, dqbuf_t buf)
  133. {
  134. memset(buf, 0, V2_DQBLKSIZE);
  135. return sb->s_op->quota_read(sb, type, (char *)buf,
  136. V2_DQBLKSIZE, blk << V2_DQBLKSIZE_BITS);
  137. }
  138. static inline ssize_t write_blk(struct super_block *sb, int type, uint blk, dqbuf_t buf)
  139. {
  140. return sb->s_op->quota_write(sb, type, (char *)buf,
  141. V2_DQBLKSIZE, blk << V2_DQBLKSIZE_BITS);
  142. }
  143. /* Remove empty block from list and return it */
  144. static int get_free_dqblk(struct super_block *sb, int type)
  145. {
  146. dqbuf_t buf = getdqbuf();
  147. struct mem_dqinfo *info = sb_dqinfo(sb, type);
  148. struct v2_disk_dqdbheader *dh = (struct v2_disk_dqdbheader *)buf;
  149. int ret, blk;
  150. if (!buf)
  151. return -ENOMEM;
  152. if (info->u.v2_i.dqi_free_blk) {
  153. blk = info->u.v2_i.dqi_free_blk;
  154. if ((ret = read_blk(sb, type, blk, buf)) < 0)
  155. goto out_buf;
  156. info->u.v2_i.dqi_free_blk = le32_to_cpu(dh->dqdh_next_free);
  157. }
  158. else {
  159. memset(buf, 0, V2_DQBLKSIZE);
  160. /* Assure block allocation... */
  161. if ((ret = write_blk(sb, type, info->u.v2_i.dqi_blocks, buf)) < 0)
  162. goto out_buf;
  163. blk = info->u.v2_i.dqi_blocks++;
  164. }
  165. mark_info_dirty(sb, type);
  166. ret = blk;
  167. out_buf:
  168. freedqbuf(buf);
  169. return ret;
  170. }
  171. /* Insert empty block to the list */
  172. static int put_free_dqblk(struct super_block *sb, int type, dqbuf_t buf, uint blk)
  173. {
  174. struct mem_dqinfo *info = sb_dqinfo(sb, type);
  175. struct v2_disk_dqdbheader *dh = (struct v2_disk_dqdbheader *)buf;
  176. int err;
  177. dh->dqdh_next_free = cpu_to_le32(info->u.v2_i.dqi_free_blk);
  178. dh->dqdh_prev_free = cpu_to_le32(0);
  179. dh->dqdh_entries = cpu_to_le16(0);
  180. info->u.v2_i.dqi_free_blk = blk;
  181. mark_info_dirty(sb, type);
  182. /* Some strange block. We had better leave it... */
  183. if ((err = write_blk(sb, type, blk, buf)) < 0)
  184. return err;
  185. return 0;
  186. }
  187. /* Remove given block from the list of blocks with free entries */
  188. static int remove_free_dqentry(struct super_block *sb, int type, dqbuf_t buf, uint blk)
  189. {
  190. dqbuf_t tmpbuf = getdqbuf();
  191. struct mem_dqinfo *info = sb_dqinfo(sb, type);
  192. struct v2_disk_dqdbheader *dh = (struct v2_disk_dqdbheader *)buf;
  193. uint nextblk = le32_to_cpu(dh->dqdh_next_free), prevblk = le32_to_cpu(dh->dqdh_prev_free);
  194. int err;
  195. if (!tmpbuf)
  196. return -ENOMEM;
  197. if (nextblk) {
  198. if ((err = read_blk(sb, type, nextblk, tmpbuf)) < 0)
  199. goto out_buf;
  200. ((struct v2_disk_dqdbheader *)tmpbuf)->dqdh_prev_free = dh->dqdh_prev_free;
  201. if ((err = write_blk(sb, type, nextblk, tmpbuf)) < 0)
  202. goto out_buf;
  203. }
  204. if (prevblk) {
  205. if ((err = read_blk(sb, type, prevblk, tmpbuf)) < 0)
  206. goto out_buf;
  207. ((struct v2_disk_dqdbheader *)tmpbuf)->dqdh_next_free = dh->dqdh_next_free;
  208. if ((err = write_blk(sb, type, prevblk, tmpbuf)) < 0)
  209. goto out_buf;
  210. }
  211. else {
  212. info->u.v2_i.dqi_free_entry = nextblk;
  213. mark_info_dirty(sb, type);
  214. }
  215. freedqbuf(tmpbuf);
  216. dh->dqdh_next_free = dh->dqdh_prev_free = cpu_to_le32(0);
  217. /* No matter whether write succeeds block is out of list */
  218. if (write_blk(sb, type, blk, buf) < 0)
  219. printk(KERN_ERR "VFS: Can't write block (%u) with free entries.\n", blk);
  220. return 0;
  221. out_buf:
  222. freedqbuf(tmpbuf);
  223. return err;
  224. }
  225. /* Insert given block to the beginning of list with free entries */
  226. static int insert_free_dqentry(struct super_block *sb, int type, dqbuf_t buf, uint blk)
  227. {
  228. dqbuf_t tmpbuf = getdqbuf();
  229. struct mem_dqinfo *info = sb_dqinfo(sb, type);
  230. struct v2_disk_dqdbheader *dh = (struct v2_disk_dqdbheader *)buf;
  231. int err;
  232. if (!tmpbuf)
  233. return -ENOMEM;
  234. dh->dqdh_next_free = cpu_to_le32(info->u.v2_i.dqi_free_entry);
  235. dh->dqdh_prev_free = cpu_to_le32(0);
  236. if ((err = write_blk(sb, type, blk, buf)) < 0)
  237. goto out_buf;
  238. if (info->u.v2_i.dqi_free_entry) {
  239. if ((err = read_blk(sb, type, info->u.v2_i.dqi_free_entry, tmpbuf)) < 0)
  240. goto out_buf;
  241. ((struct v2_disk_dqdbheader *)tmpbuf)->dqdh_prev_free = cpu_to_le32(blk);
  242. if ((err = write_blk(sb, type, info->u.v2_i.dqi_free_entry, tmpbuf)) < 0)
  243. goto out_buf;
  244. }
  245. freedqbuf(tmpbuf);
  246. info->u.v2_i.dqi_free_entry = blk;
  247. mark_info_dirty(sb, type);
  248. return 0;
  249. out_buf:
  250. freedqbuf(tmpbuf);
  251. return err;
  252. }
  253. /* Find space for dquot */
  254. static uint find_free_dqentry(struct dquot *dquot, int *err)
  255. {
  256. struct super_block *sb = dquot->dq_sb;
  257. struct mem_dqinfo *info = sb_dqopt(sb)->info+dquot->dq_type;
  258. uint blk, i;
  259. struct v2_disk_dqdbheader *dh;
  260. struct v2_disk_dqblk *ddquot;
  261. struct v2_disk_dqblk fakedquot;
  262. dqbuf_t buf;
  263. *err = 0;
  264. if (!(buf = getdqbuf())) {
  265. *err = -ENOMEM;
  266. return 0;
  267. }
  268. dh = (struct v2_disk_dqdbheader *)buf;
  269. ddquot = GETENTRIES(buf);
  270. if (info->u.v2_i.dqi_free_entry) {
  271. blk = info->u.v2_i.dqi_free_entry;
  272. if ((*err = read_blk(sb, dquot->dq_type, blk, buf)) < 0)
  273. goto out_buf;
  274. }
  275. else {
  276. blk = get_free_dqblk(sb, dquot->dq_type);
  277. if ((int)blk < 0) {
  278. *err = blk;
  279. freedqbuf(buf);
  280. return 0;
  281. }
  282. memset(buf, 0, V2_DQBLKSIZE);
  283. /* This is enough as block is already zeroed and entry list is empty... */
  284. info->u.v2_i.dqi_free_entry = blk;
  285. mark_info_dirty(sb, dquot->dq_type);
  286. }
  287. if (le16_to_cpu(dh->dqdh_entries)+1 >= V2_DQSTRINBLK) /* Block will be full? */
  288. if ((*err = remove_free_dqentry(sb, dquot->dq_type, buf, blk)) < 0) {
  289. printk(KERN_ERR "VFS: find_free_dqentry(): Can't remove block (%u) from entry free list.\n", blk);
  290. goto out_buf;
  291. }
  292. le16_add_cpu(&dh->dqdh_entries, 1);
  293. memset(&fakedquot, 0, sizeof(struct v2_disk_dqblk));
  294. /* Find free structure in block */
  295. for (i = 0; i < V2_DQSTRINBLK && memcmp(&fakedquot, ddquot+i, sizeof(struct v2_disk_dqblk)); i++);
  296. #ifdef __QUOTA_V2_PARANOIA
  297. if (i == V2_DQSTRINBLK) {
  298. printk(KERN_ERR "VFS: find_free_dqentry(): Data block full but it shouldn't.\n");
  299. *err = -EIO;
  300. goto out_buf;
  301. }
  302. #endif
  303. if ((*err = write_blk(sb, dquot->dq_type, blk, buf)) < 0) {
  304. printk(KERN_ERR "VFS: find_free_dqentry(): Can't write quota data block %u.\n", blk);
  305. goto out_buf;
  306. }
  307. dquot->dq_off = (blk<<V2_DQBLKSIZE_BITS)+sizeof(struct v2_disk_dqdbheader)+i*sizeof(struct v2_disk_dqblk);
  308. freedqbuf(buf);
  309. return blk;
  310. out_buf:
  311. freedqbuf(buf);
  312. return 0;
  313. }
  314. /* Insert reference to structure into the trie */
  315. static int do_insert_tree(struct dquot *dquot, uint *treeblk, int depth)
  316. {
  317. struct super_block *sb = dquot->dq_sb;
  318. dqbuf_t buf;
  319. int ret = 0, newson = 0, newact = 0;
  320. __le32 *ref;
  321. uint newblk;
  322. if (!(buf = getdqbuf()))
  323. return -ENOMEM;
  324. if (!*treeblk) {
  325. ret = get_free_dqblk(sb, dquot->dq_type);
  326. if (ret < 0)
  327. goto out_buf;
  328. *treeblk = ret;
  329. memset(buf, 0, V2_DQBLKSIZE);
  330. newact = 1;
  331. }
  332. else {
  333. if ((ret = read_blk(sb, dquot->dq_type, *treeblk, buf)) < 0) {
  334. printk(KERN_ERR "VFS: Can't read tree quota block %u.\n", *treeblk);
  335. goto out_buf;
  336. }
  337. }
  338. ref = (__le32 *)buf;
  339. newblk = le32_to_cpu(ref[GETIDINDEX(dquot->dq_id, depth)]);
  340. if (!newblk)
  341. newson = 1;
  342. if (depth == V2_DQTREEDEPTH-1) {
  343. #ifdef __QUOTA_V2_PARANOIA
  344. if (newblk) {
  345. printk(KERN_ERR "VFS: Inserting already present quota entry (block %u).\n", le32_to_cpu(ref[GETIDINDEX(dquot->dq_id, depth)]));
  346. ret = -EIO;
  347. goto out_buf;
  348. }
  349. #endif
  350. newblk = find_free_dqentry(dquot, &ret);
  351. }
  352. else
  353. ret = do_insert_tree(dquot, &newblk, depth+1);
  354. if (newson && ret >= 0) {
  355. ref[GETIDINDEX(dquot->dq_id, depth)] = cpu_to_le32(newblk);
  356. ret = write_blk(sb, dquot->dq_type, *treeblk, buf);
  357. }
  358. else if (newact && ret < 0)
  359. put_free_dqblk(sb, dquot->dq_type, buf, *treeblk);
  360. out_buf:
  361. freedqbuf(buf);
  362. return ret;
  363. }
  364. /* Wrapper for inserting quota structure into tree */
  365. static inline int dq_insert_tree(struct dquot *dquot)
  366. {
  367. int tmp = V2_DQTREEOFF;
  368. return do_insert_tree(dquot, &tmp, 0);
  369. }
  370. /*
  371. * We don't have to be afraid of deadlocks as we never have quotas on quota files...
  372. */
  373. static int v2_write_dquot(struct dquot *dquot)
  374. {
  375. int type = dquot->dq_type;
  376. ssize_t ret;
  377. struct v2_disk_dqblk ddquot, empty;
  378. /* dq_off is guarded by dqio_mutex */
  379. if (!dquot->dq_off)
  380. if ((ret = dq_insert_tree(dquot)) < 0) {
  381. printk(KERN_ERR "VFS: Error %zd occurred while creating quota.\n", ret);
  382. return ret;
  383. }
  384. spin_lock(&dq_data_lock);
  385. mem2diskdqb(&ddquot, &dquot->dq_dqb, dquot->dq_id);
  386. /* Argh... We may need to write structure full of zeroes but that would be
  387. * treated as an empty place by the rest of the code. Format change would
  388. * be definitely cleaner but the problems probably are not worth it */
  389. memset(&empty, 0, sizeof(struct v2_disk_dqblk));
  390. if (!memcmp(&empty, &ddquot, sizeof(struct v2_disk_dqblk)))
  391. ddquot.dqb_itime = cpu_to_le64(1);
  392. spin_unlock(&dq_data_lock);
  393. ret = dquot->dq_sb->s_op->quota_write(dquot->dq_sb, type,
  394. (char *)&ddquot, sizeof(struct v2_disk_dqblk), dquot->dq_off);
  395. if (ret != sizeof(struct v2_disk_dqblk)) {
  396. printk(KERN_WARNING "VFS: dquota write failed on dev %s\n", dquot->dq_sb->s_id);
  397. if (ret >= 0)
  398. ret = -ENOSPC;
  399. }
  400. else
  401. ret = 0;
  402. dqstats.writes++;
  403. return ret;
  404. }
  405. /* Free dquot entry in data block */
  406. static int free_dqentry(struct dquot *dquot, uint blk)
  407. {
  408. struct super_block *sb = dquot->dq_sb;
  409. int type = dquot->dq_type;
  410. struct v2_disk_dqdbheader *dh;
  411. dqbuf_t buf = getdqbuf();
  412. int ret = 0;
  413. if (!buf)
  414. return -ENOMEM;
  415. if (dquot->dq_off >> V2_DQBLKSIZE_BITS != blk) {
  416. printk(KERN_ERR "VFS: Quota structure has offset to other "
  417. "block (%u) than it should (%u).\n", blk,
  418. (uint)(dquot->dq_off >> V2_DQBLKSIZE_BITS));
  419. goto out_buf;
  420. }
  421. if ((ret = read_blk(sb, type, blk, buf)) < 0) {
  422. printk(KERN_ERR "VFS: Can't read quota data block %u\n", blk);
  423. goto out_buf;
  424. }
  425. dh = (struct v2_disk_dqdbheader *)buf;
  426. le16_add_cpu(&dh->dqdh_entries, -1);
  427. if (!le16_to_cpu(dh->dqdh_entries)) { /* Block got free? */
  428. if ((ret = remove_free_dqentry(sb, type, buf, blk)) < 0 ||
  429. (ret = put_free_dqblk(sb, type, buf, blk)) < 0) {
  430. printk(KERN_ERR "VFS: Can't move quota data block (%u) "
  431. "to free list.\n", blk);
  432. goto out_buf;
  433. }
  434. }
  435. else {
  436. memset(buf+(dquot->dq_off & ((1 << V2_DQBLKSIZE_BITS)-1)), 0,
  437. sizeof(struct v2_disk_dqblk));
  438. if (le16_to_cpu(dh->dqdh_entries) == V2_DQSTRINBLK-1) {
  439. /* Insert will write block itself */
  440. if ((ret = insert_free_dqentry(sb, type, buf, blk)) < 0) {
  441. printk(KERN_ERR "VFS: Can't insert quota data block (%u) to free entry list.\n", blk);
  442. goto out_buf;
  443. }
  444. }
  445. else
  446. if ((ret = write_blk(sb, type, blk, buf)) < 0) {
  447. printk(KERN_ERR "VFS: Can't write quota data "
  448. "block %u\n", blk);
  449. goto out_buf;
  450. }
  451. }
  452. dquot->dq_off = 0; /* Quota is now unattached */
  453. out_buf:
  454. freedqbuf(buf);
  455. return ret;
  456. }
  457. /* Remove reference to dquot from tree */
  458. static int remove_tree(struct dquot *dquot, uint *blk, int depth)
  459. {
  460. struct super_block *sb = dquot->dq_sb;
  461. int type = dquot->dq_type;
  462. dqbuf_t buf = getdqbuf();
  463. int ret = 0;
  464. uint newblk;
  465. __le32 *ref = (__le32 *)buf;
  466. if (!buf)
  467. return -ENOMEM;
  468. if ((ret = read_blk(sb, type, *blk, buf)) < 0) {
  469. printk(KERN_ERR "VFS: Can't read quota data block %u\n", *blk);
  470. goto out_buf;
  471. }
  472. newblk = le32_to_cpu(ref[GETIDINDEX(dquot->dq_id, depth)]);
  473. if (depth == V2_DQTREEDEPTH-1) {
  474. ret = free_dqentry(dquot, newblk);
  475. newblk = 0;
  476. }
  477. else
  478. ret = remove_tree(dquot, &newblk, depth+1);
  479. if (ret >= 0 && !newblk) {
  480. int i;
  481. ref[GETIDINDEX(dquot->dq_id, depth)] = cpu_to_le32(0);
  482. for (i = 0; i < V2_DQBLKSIZE && !buf[i]; i++); /* Block got empty? */
  483. /* Don't put the root block into the free block list */
  484. if (i == V2_DQBLKSIZE && *blk != V2_DQTREEOFF) {
  485. put_free_dqblk(sb, type, buf, *blk);
  486. *blk = 0;
  487. }
  488. else
  489. if ((ret = write_blk(sb, type, *blk, buf)) < 0)
  490. printk(KERN_ERR "VFS: Can't write quota tree "
  491. "block %u.\n", *blk);
  492. }
  493. out_buf:
  494. freedqbuf(buf);
  495. return ret;
  496. }
  497. /* Delete dquot from tree */
  498. static int v2_delete_dquot(struct dquot *dquot)
  499. {
  500. uint tmp = V2_DQTREEOFF;
  501. if (!dquot->dq_off) /* Even not allocated? */
  502. return 0;
  503. return remove_tree(dquot, &tmp, 0);
  504. }
  505. /* Find entry in block */
  506. static loff_t find_block_dqentry(struct dquot *dquot, uint blk)
  507. {
  508. dqbuf_t buf = getdqbuf();
  509. loff_t ret = 0;
  510. int i;
  511. struct v2_disk_dqblk *ddquot = GETENTRIES(buf);
  512. if (!buf)
  513. return -ENOMEM;
  514. if ((ret = read_blk(dquot->dq_sb, dquot->dq_type, blk, buf)) < 0) {
  515. printk(KERN_ERR "VFS: Can't read quota tree block %u.\n", blk);
  516. goto out_buf;
  517. }
  518. if (dquot->dq_id)
  519. for (i = 0; i < V2_DQSTRINBLK &&
  520. le32_to_cpu(ddquot[i].dqb_id) != dquot->dq_id; i++);
  521. else { /* ID 0 as a bit more complicated searching... */
  522. struct v2_disk_dqblk fakedquot;
  523. memset(&fakedquot, 0, sizeof(struct v2_disk_dqblk));
  524. for (i = 0; i < V2_DQSTRINBLK; i++)
  525. if (!le32_to_cpu(ddquot[i].dqb_id) &&
  526. memcmp(&fakedquot, ddquot+i, sizeof(struct v2_disk_dqblk)))
  527. break;
  528. }
  529. if (i == V2_DQSTRINBLK) {
  530. printk(KERN_ERR "VFS: Quota for id %u referenced "
  531. "but not present.\n", dquot->dq_id);
  532. ret = -EIO;
  533. goto out_buf;
  534. }
  535. else
  536. ret = (blk << V2_DQBLKSIZE_BITS) + sizeof(struct
  537. v2_disk_dqdbheader) + i * sizeof(struct v2_disk_dqblk);
  538. out_buf:
  539. freedqbuf(buf);
  540. return ret;
  541. }
  542. /* Find entry for given id in the tree */
  543. static loff_t find_tree_dqentry(struct dquot *dquot, uint blk, int depth)
  544. {
  545. dqbuf_t buf = getdqbuf();
  546. loff_t ret = 0;
  547. __le32 *ref = (__le32 *)buf;
  548. if (!buf)
  549. return -ENOMEM;
  550. if ((ret = read_blk(dquot->dq_sb, dquot->dq_type, blk, buf)) < 0) {
  551. printk(KERN_ERR "VFS: Can't read quota tree block %u.\n", blk);
  552. goto out_buf;
  553. }
  554. ret = 0;
  555. blk = le32_to_cpu(ref[GETIDINDEX(dquot->dq_id, depth)]);
  556. if (!blk) /* No reference? */
  557. goto out_buf;
  558. if (depth < V2_DQTREEDEPTH-1)
  559. ret = find_tree_dqentry(dquot, blk, depth+1);
  560. else
  561. ret = find_block_dqentry(dquot, blk);
  562. out_buf:
  563. freedqbuf(buf);
  564. return ret;
  565. }
  566. /* Find entry for given id in the tree - wrapper function */
  567. static inline loff_t find_dqentry(struct dquot *dquot)
  568. {
  569. return find_tree_dqentry(dquot, V2_DQTREEOFF, 0);
  570. }
  571. static int v2_read_dquot(struct dquot *dquot)
  572. {
  573. int type = dquot->dq_type;
  574. loff_t offset;
  575. struct v2_disk_dqblk ddquot, empty;
  576. int ret = 0;
  577. #ifdef __QUOTA_V2_PARANOIA
  578. /* Invalidated quota? */
  579. if (!dquot->dq_sb || !sb_dqopt(dquot->dq_sb)->files[type]) {
  580. printk(KERN_ERR "VFS: Quota invalidated while reading!\n");
  581. return -EIO;
  582. }
  583. #endif
  584. offset = find_dqentry(dquot);
  585. if (offset <= 0) { /* Entry not present? */
  586. if (offset < 0)
  587. printk(KERN_ERR "VFS: Can't read quota "
  588. "structure for id %u.\n", dquot->dq_id);
  589. dquot->dq_off = 0;
  590. set_bit(DQ_FAKE_B, &dquot->dq_flags);
  591. memset(&dquot->dq_dqb, 0, sizeof(struct mem_dqblk));
  592. ret = offset;
  593. }
  594. else {
  595. dquot->dq_off = offset;
  596. if ((ret = dquot->dq_sb->s_op->quota_read(dquot->dq_sb, type,
  597. (char *)&ddquot, sizeof(struct v2_disk_dqblk), offset))
  598. != sizeof(struct v2_disk_dqblk)) {
  599. if (ret >= 0)
  600. ret = -EIO;
  601. printk(KERN_ERR "VFS: Error while reading quota "
  602. "structure for id %u.\n", dquot->dq_id);
  603. memset(&ddquot, 0, sizeof(struct v2_disk_dqblk));
  604. }
  605. else {
  606. ret = 0;
  607. /* We need to escape back all-zero structure */
  608. memset(&empty, 0, sizeof(struct v2_disk_dqblk));
  609. empty.dqb_itime = cpu_to_le64(1);
  610. if (!memcmp(&empty, &ddquot, sizeof(struct v2_disk_dqblk)))
  611. ddquot.dqb_itime = 0;
  612. }
  613. disk2memdqb(&dquot->dq_dqb, &ddquot);
  614. if (!dquot->dq_dqb.dqb_bhardlimit &&
  615. !dquot->dq_dqb.dqb_bsoftlimit &&
  616. !dquot->dq_dqb.dqb_ihardlimit &&
  617. !dquot->dq_dqb.dqb_isoftlimit)
  618. set_bit(DQ_FAKE_B, &dquot->dq_flags);
  619. }
  620. dqstats.reads++;
  621. return ret;
  622. }
  623. /* Check whether dquot should not be deleted. We know we are
  624. * the only one operating on dquot (thanks to dq_lock) */
  625. static int v2_release_dquot(struct dquot *dquot)
  626. {
  627. if (test_bit(DQ_FAKE_B, &dquot->dq_flags) && !(dquot->dq_dqb.dqb_curinodes | dquot->dq_dqb.dqb_curspace))
  628. return v2_delete_dquot(dquot);
  629. return 0;
  630. }
  631. static struct quota_format_ops v2_format_ops = {
  632. .check_quota_file = v2_check_quota_file,
  633. .read_file_info = v2_read_file_info,
  634. .write_file_info = v2_write_file_info,
  635. .free_file_info = NULL,
  636. .read_dqblk = v2_read_dquot,
  637. .commit_dqblk = v2_write_dquot,
  638. .release_dqblk = v2_release_dquot,
  639. };
  640. static struct quota_format_type v2_quota_format = {
  641. .qf_fmt_id = QFMT_VFS_V0,
  642. .qf_ops = &v2_format_ops,
  643. .qf_owner = THIS_MODULE
  644. };
  645. static int __init init_v2_quota_format(void)
  646. {
  647. return register_quota_format(&v2_quota_format);
  648. }
  649. static void __exit exit_v2_quota_format(void)
  650. {
  651. unregister_quota_format(&v2_quota_format);
  652. }
  653. module_init(init_v2_quota_format);
  654. module_exit(exit_v2_quota_format);