ea.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. /*
  2. * linux/fs/hpfs/ea.c
  3. *
  4. * Mikulas Patocka (mikulas@artax.karlin.mff.cuni.cz), 1998-1999
  5. *
  6. * handling extended attributes
  7. */
  8. #include "hpfs_fn.h"
  9. /* Remove external extended attributes. ano specifies whether a is a
  10. direct sector where eas starts or an anode */
  11. void hpfs_ea_ext_remove(struct super_block *s, secno a, int ano, unsigned len)
  12. {
  13. unsigned pos = 0;
  14. while (pos < len) {
  15. char ex[4 + 255 + 1 + 8];
  16. struct extended_attribute *ea = (struct extended_attribute *)ex;
  17. if (pos + 4 > len) {
  18. hpfs_error(s, "EAs don't end correctly, %s %08x, len %08x",
  19. ano ? "anode" : "sectors", a, len);
  20. return;
  21. }
  22. if (hpfs_ea_read(s, a, ano, pos, 4, ex)) return;
  23. if (ea_indirect(ea)) {
  24. if (ea_valuelen(ea) != 8) {
  25. hpfs_error(s, "ea_indirect(ea) set while ea->valuelen!=8, %s %08x, pos %08x",
  26. ano ? "anode" : "sectors", a, pos);
  27. return;
  28. }
  29. if (hpfs_ea_read(s, a, ano, pos + 4, ea->namelen + 9, ex+4))
  30. return;
  31. hpfs_ea_remove(s, ea_sec(ea), ea_in_anode(ea), ea_len(ea));
  32. }
  33. pos += ea->namelen + ea_valuelen(ea) + 5;
  34. }
  35. if (!ano) hpfs_free_sectors(s, a, (len+511) >> 9);
  36. else {
  37. struct buffer_head *bh;
  38. struct anode *anode;
  39. if ((anode = hpfs_map_anode(s, a, &bh))) {
  40. hpfs_remove_btree(s, &anode->btree);
  41. brelse(bh);
  42. hpfs_free_sectors(s, a, 1);
  43. }
  44. }
  45. }
  46. static char *get_indirect_ea(struct super_block *s, int ano, secno a, int size)
  47. {
  48. char *ret;
  49. if (!(ret = kmalloc(size + 1, GFP_NOFS))) {
  50. pr_err("out of memory for EA\n");
  51. return NULL;
  52. }
  53. if (hpfs_ea_read(s, a, ano, 0, size, ret)) {
  54. kfree(ret);
  55. return NULL;
  56. }
  57. ret[size] = 0;
  58. return ret;
  59. }
  60. static void set_indirect_ea(struct super_block *s, int ano, secno a,
  61. const char *data, int size)
  62. {
  63. hpfs_ea_write(s, a, ano, 0, size, data);
  64. }
  65. /* Read an extended attribute named 'key' into the provided buffer */
  66. int hpfs_read_ea(struct super_block *s, struct fnode *fnode, char *key,
  67. char *buf, int size)
  68. {
  69. unsigned pos;
  70. int ano, len;
  71. secno a;
  72. char ex[4 + 255 + 1 + 8];
  73. struct extended_attribute *ea;
  74. struct extended_attribute *ea_end = fnode_end_ea(fnode);
  75. for (ea = fnode_ea(fnode); ea < ea_end; ea = next_ea(ea))
  76. if (!strcmp(ea->name, key)) {
  77. if (ea_indirect(ea))
  78. goto indirect;
  79. if (ea_valuelen(ea) >= size)
  80. return -EINVAL;
  81. memcpy(buf, ea_data(ea), ea_valuelen(ea));
  82. buf[ea_valuelen(ea)] = 0;
  83. return 0;
  84. }
  85. a = le32_to_cpu(fnode->ea_secno);
  86. len = le32_to_cpu(fnode->ea_size_l);
  87. ano = fnode_in_anode(fnode);
  88. pos = 0;
  89. while (pos < len) {
  90. ea = (struct extended_attribute *)ex;
  91. if (pos + 4 > len) {
  92. hpfs_error(s, "EAs don't end correctly, %s %08x, len %08x",
  93. ano ? "anode" : "sectors", a, len);
  94. return -EIO;
  95. }
  96. if (hpfs_ea_read(s, a, ano, pos, 4, ex)) return -EIO;
  97. if (hpfs_ea_read(s, a, ano, pos + 4, ea->namelen + 1 + (ea_indirect(ea) ? 8 : 0), ex + 4))
  98. return -EIO;
  99. if (!strcmp(ea->name, key)) {
  100. if (ea_indirect(ea))
  101. goto indirect;
  102. if (ea_valuelen(ea) >= size)
  103. return -EINVAL;
  104. if (hpfs_ea_read(s, a, ano, pos + 4 + ea->namelen + 1, ea_valuelen(ea), buf))
  105. return -EIO;
  106. buf[ea_valuelen(ea)] = 0;
  107. return 0;
  108. }
  109. pos += ea->namelen + ea_valuelen(ea) + 5;
  110. }
  111. return -ENOENT;
  112. indirect:
  113. if (ea_len(ea) >= size)
  114. return -EINVAL;
  115. if (hpfs_ea_read(s, ea_sec(ea), ea_in_anode(ea), 0, ea_len(ea), buf))
  116. return -EIO;
  117. buf[ea_len(ea)] = 0;
  118. return 0;
  119. }
  120. /* Read an extended attribute named 'key' */
  121. char *hpfs_get_ea(struct super_block *s, struct fnode *fnode, char *key, int *size)
  122. {
  123. char *ret;
  124. unsigned pos;
  125. int ano, len;
  126. secno a;
  127. struct extended_attribute *ea;
  128. struct extended_attribute *ea_end = fnode_end_ea(fnode);
  129. for (ea = fnode_ea(fnode); ea < ea_end; ea = next_ea(ea))
  130. if (!strcmp(ea->name, key)) {
  131. if (ea_indirect(ea))
  132. return get_indirect_ea(s, ea_in_anode(ea), ea_sec(ea), *size = ea_len(ea));
  133. if (!(ret = kmalloc((*size = ea_valuelen(ea)) + 1, GFP_NOFS))) {
  134. pr_err("out of memory for EA\n");
  135. return NULL;
  136. }
  137. memcpy(ret, ea_data(ea), ea_valuelen(ea));
  138. ret[ea_valuelen(ea)] = 0;
  139. return ret;
  140. }
  141. a = le32_to_cpu(fnode->ea_secno);
  142. len = le32_to_cpu(fnode->ea_size_l);
  143. ano = fnode_in_anode(fnode);
  144. pos = 0;
  145. while (pos < len) {
  146. char ex[4 + 255 + 1 + 8];
  147. ea = (struct extended_attribute *)ex;
  148. if (pos + 4 > len) {
  149. hpfs_error(s, "EAs don't end correctly, %s %08x, len %08x",
  150. ano ? "anode" : "sectors", a, len);
  151. return NULL;
  152. }
  153. if (hpfs_ea_read(s, a, ano, pos, 4, ex)) return NULL;
  154. if (hpfs_ea_read(s, a, ano, pos + 4, ea->namelen + 1 + (ea_indirect(ea) ? 8 : 0), ex + 4))
  155. return NULL;
  156. if (!strcmp(ea->name, key)) {
  157. if (ea_indirect(ea))
  158. return get_indirect_ea(s, ea_in_anode(ea), ea_sec(ea), *size = ea_len(ea));
  159. if (!(ret = kmalloc((*size = ea_valuelen(ea)) + 1, GFP_NOFS))) {
  160. pr_err("out of memory for EA\n");
  161. return NULL;
  162. }
  163. if (hpfs_ea_read(s, a, ano, pos + 4 + ea->namelen + 1, ea_valuelen(ea), ret)) {
  164. kfree(ret);
  165. return NULL;
  166. }
  167. ret[ea_valuelen(ea)] = 0;
  168. return ret;
  169. }
  170. pos += ea->namelen + ea_valuelen(ea) + 5;
  171. }
  172. return NULL;
  173. }
  174. /*
  175. * Update or create extended attribute 'key' with value 'data'. Note that
  176. * when this ea exists, it MUST have the same size as size of data.
  177. * This driver can't change sizes of eas ('cause I just don't need it).
  178. */
  179. void hpfs_set_ea(struct inode *inode, struct fnode *fnode, const char *key,
  180. const char *data, int size)
  181. {
  182. fnode_secno fno = inode->i_ino;
  183. struct super_block *s = inode->i_sb;
  184. unsigned pos;
  185. int ano, len;
  186. secno a;
  187. unsigned char h[4];
  188. struct extended_attribute *ea;
  189. struct extended_attribute *ea_end = fnode_end_ea(fnode);
  190. for (ea = fnode_ea(fnode); ea < ea_end; ea = next_ea(ea))
  191. if (!strcmp(ea->name, key)) {
  192. if (ea_indirect(ea)) {
  193. if (ea_len(ea) == size)
  194. set_indirect_ea(s, ea_in_anode(ea), ea_sec(ea), data, size);
  195. } else if (ea_valuelen(ea) == size) {
  196. memcpy(ea_data(ea), data, size);
  197. }
  198. return;
  199. }
  200. a = le32_to_cpu(fnode->ea_secno);
  201. len = le32_to_cpu(fnode->ea_size_l);
  202. ano = fnode_in_anode(fnode);
  203. pos = 0;
  204. while (pos < len) {
  205. char ex[4 + 255 + 1 + 8];
  206. ea = (struct extended_attribute *)ex;
  207. if (pos + 4 > len) {
  208. hpfs_error(s, "EAs don't end correctly, %s %08x, len %08x",
  209. ano ? "anode" : "sectors", a, len);
  210. return;
  211. }
  212. if (hpfs_ea_read(s, a, ano, pos, 4, ex)) return;
  213. if (hpfs_ea_read(s, a, ano, pos + 4, ea->namelen + 1 + (ea_indirect(ea) ? 8 : 0), ex + 4))
  214. return;
  215. if (!strcmp(ea->name, key)) {
  216. if (ea_indirect(ea)) {
  217. if (ea_len(ea) == size)
  218. set_indirect_ea(s, ea_in_anode(ea), ea_sec(ea), data, size);
  219. }
  220. else {
  221. if (ea_valuelen(ea) == size)
  222. hpfs_ea_write(s, a, ano, pos + 4 + ea->namelen + 1, size, data);
  223. }
  224. return;
  225. }
  226. pos += ea->namelen + ea_valuelen(ea) + 5;
  227. }
  228. if (!le16_to_cpu(fnode->ea_offs)) {
  229. /*if (le16_to_cpu(fnode->ea_size_s)) {
  230. hpfs_error(s, "fnode %08x: ea_size_s == %03x, ea_offs == 0",
  231. inode->i_ino, le16_to_cpu(fnode->ea_size_s));
  232. return;
  233. }*/
  234. fnode->ea_offs = cpu_to_le16(0xc4);
  235. }
  236. if (le16_to_cpu(fnode->ea_offs) < 0xc4 || le16_to_cpu(fnode->ea_offs) + le16_to_cpu(fnode->acl_size_s) + le16_to_cpu(fnode->ea_size_s) > 0x200) {
  237. hpfs_error(s, "fnode %08lx: ea_offs == %03x, ea_size_s == %03x",
  238. (unsigned long)inode->i_ino,
  239. le16_to_cpu(fnode->ea_offs), le16_to_cpu(fnode->ea_size_s));
  240. return;
  241. }
  242. if ((le16_to_cpu(fnode->ea_size_s) || !le32_to_cpu(fnode->ea_size_l)) &&
  243. le16_to_cpu(fnode->ea_offs) + le16_to_cpu(fnode->acl_size_s) + le16_to_cpu(fnode->ea_size_s) + strlen(key) + size + 5 <= 0x200) {
  244. ea = fnode_end_ea(fnode);
  245. *(char *)ea = 0;
  246. ea->namelen = strlen(key);
  247. ea->valuelen_lo = size;
  248. ea->valuelen_hi = size >> 8;
  249. strcpy(ea->name, key);
  250. memcpy(ea_data(ea), data, size);
  251. fnode->ea_size_s = cpu_to_le16(le16_to_cpu(fnode->ea_size_s) + strlen(key) + size + 5);
  252. goto ret;
  253. }
  254. /* Most the code here is 99.9993422% unused. I hope there are no bugs.
  255. But what .. HPFS.IFS has also bugs in ea management. */
  256. if (le16_to_cpu(fnode->ea_size_s) && !le32_to_cpu(fnode->ea_size_l)) {
  257. secno n;
  258. struct buffer_head *bh;
  259. char *data;
  260. if (!(n = hpfs_alloc_sector(s, fno, 1, 0))) return;
  261. if (!(data = hpfs_get_sector(s, n, &bh))) {
  262. hpfs_free_sectors(s, n, 1);
  263. return;
  264. }
  265. memcpy(data, fnode_ea(fnode), le16_to_cpu(fnode->ea_size_s));
  266. fnode->ea_size_l = cpu_to_le32(le16_to_cpu(fnode->ea_size_s));
  267. fnode->ea_size_s = cpu_to_le16(0);
  268. fnode->ea_secno = cpu_to_le32(n);
  269. fnode->flags &= ~FNODE_anode;
  270. mark_buffer_dirty(bh);
  271. brelse(bh);
  272. }
  273. pos = le32_to_cpu(fnode->ea_size_l) + 5 + strlen(key) + size;
  274. len = (le32_to_cpu(fnode->ea_size_l) + 511) >> 9;
  275. if (pos >= 30000) goto bail;
  276. while (((pos + 511) >> 9) > len) {
  277. if (!len) {
  278. secno q = hpfs_alloc_sector(s, fno, 1, 0);
  279. if (!q) goto bail;
  280. fnode->ea_secno = cpu_to_le32(q);
  281. fnode->flags &= ~FNODE_anode;
  282. len++;
  283. } else if (!fnode_in_anode(fnode)) {
  284. if (hpfs_alloc_if_possible(s, le32_to_cpu(fnode->ea_secno) + len)) {
  285. len++;
  286. } else {
  287. /* Aargh... don't know how to create ea anodes :-( */
  288. /*struct buffer_head *bh;
  289. struct anode *anode;
  290. anode_secno a_s;
  291. if (!(anode = hpfs_alloc_anode(s, fno, &a_s, &bh)))
  292. goto bail;
  293. anode->up = cpu_to_le32(fno);
  294. anode->btree.fnode_parent = 1;
  295. anode->btree.n_free_nodes--;
  296. anode->btree.n_used_nodes++;
  297. anode->btree.first_free = cpu_to_le16(le16_to_cpu(anode->btree.first_free) + 12);
  298. anode->u.external[0].disk_secno = cpu_to_le32(le32_to_cpu(fnode->ea_secno));
  299. anode->u.external[0].file_secno = cpu_to_le32(0);
  300. anode->u.external[0].length = cpu_to_le32(len);
  301. mark_buffer_dirty(bh);
  302. brelse(bh);
  303. fnode->flags |= FNODE_anode;
  304. fnode->ea_secno = cpu_to_le32(a_s);*/
  305. secno new_sec;
  306. int i;
  307. if (!(new_sec = hpfs_alloc_sector(s, fno, 1, 1 - ((pos + 511) >> 9))))
  308. goto bail;
  309. for (i = 0; i < len; i++) {
  310. struct buffer_head *bh1, *bh2;
  311. void *b1, *b2;
  312. if (!(b1 = hpfs_map_sector(s, le32_to_cpu(fnode->ea_secno) + i, &bh1, len - i - 1))) {
  313. hpfs_free_sectors(s, new_sec, (pos + 511) >> 9);
  314. goto bail;
  315. }
  316. if (!(b2 = hpfs_get_sector(s, new_sec + i, &bh2))) {
  317. brelse(bh1);
  318. hpfs_free_sectors(s, new_sec, (pos + 511) >> 9);
  319. goto bail;
  320. }
  321. memcpy(b2, b1, 512);
  322. brelse(bh1);
  323. mark_buffer_dirty(bh2);
  324. brelse(bh2);
  325. }
  326. hpfs_free_sectors(s, le32_to_cpu(fnode->ea_secno), len);
  327. fnode->ea_secno = cpu_to_le32(new_sec);
  328. len = (pos + 511) >> 9;
  329. }
  330. }
  331. if (fnode_in_anode(fnode)) {
  332. if (hpfs_add_sector_to_btree(s, le32_to_cpu(fnode->ea_secno),
  333. 0, len) != -1) {
  334. len++;
  335. } else {
  336. goto bail;
  337. }
  338. }
  339. }
  340. h[0] = 0;
  341. h[1] = strlen(key);
  342. h[2] = size & 0xff;
  343. h[3] = size >> 8;
  344. if (hpfs_ea_write(s, le32_to_cpu(fnode->ea_secno), fnode_in_anode(fnode), le32_to_cpu(fnode->ea_size_l), 4, h)) goto bail;
  345. if (hpfs_ea_write(s, le32_to_cpu(fnode->ea_secno), fnode_in_anode(fnode), le32_to_cpu(fnode->ea_size_l) + 4, h[1] + 1, key)) goto bail;
  346. if (hpfs_ea_write(s, le32_to_cpu(fnode->ea_secno), fnode_in_anode(fnode), le32_to_cpu(fnode->ea_size_l) + 5 + h[1], size, data)) goto bail;
  347. fnode->ea_size_l = cpu_to_le32(pos);
  348. ret:
  349. hpfs_i(inode)->i_ea_size += 5 + strlen(key) + size;
  350. return;
  351. bail:
  352. if (le32_to_cpu(fnode->ea_secno))
  353. if (fnode_in_anode(fnode)) hpfs_truncate_btree(s, le32_to_cpu(fnode->ea_secno), 1, (le32_to_cpu(fnode->ea_size_l) + 511) >> 9);
  354. else hpfs_free_sectors(s, le32_to_cpu(fnode->ea_secno) + ((le32_to_cpu(fnode->ea_size_l) + 511) >> 9), len - ((le32_to_cpu(fnode->ea_size_l) + 511) >> 9));
  355. else fnode->ea_secno = fnode->ea_size_l = cpu_to_le32(0);
  356. }