quota_v2.c 20 KB

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