quota_v2.c 20 KB

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