xattr.c 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726
  1. /*
  2. * linux/fs/ext4/xattr.c
  3. *
  4. * Copyright (C) 2001-2003 Andreas Gruenbacher, <agruen@suse.de>
  5. *
  6. * Fix by Harrison Xing <harrison@mountainviewdata.com>.
  7. * Ext4 code with a lot of help from Eric Jarman <ejarman@acm.org>.
  8. * Extended attributes for symlinks and special files added per
  9. * suggestion of Luka Renko <luka.renko@hermes.si>.
  10. * xattr consolidation Copyright (c) 2004 James Morris <jmorris@redhat.com>,
  11. * Red Hat Inc.
  12. * ea-in-inode support by Alex Tomas <alex@clusterfs.com> aka bzzz
  13. * and Andreas Gruenbacher <agruen@suse.de>.
  14. */
  15. /*
  16. * Extended attributes are stored directly in inodes (on file systems with
  17. * inodes bigger than 128 bytes) and on additional disk blocks. The i_file_acl
  18. * field contains the block number if an inode uses an additional block. All
  19. * attributes must fit in the inode and one additional block. Blocks that
  20. * contain the identical set of attributes may be shared among several inodes.
  21. * Identical blocks are detected by keeping a cache of blocks that have
  22. * recently been accessed.
  23. *
  24. * The attributes in inodes and on blocks have a different header; the entries
  25. * are stored in the same format:
  26. *
  27. * +------------------+
  28. * | header |
  29. * | entry 1 | |
  30. * | entry 2 | | growing downwards
  31. * | entry 3 | v
  32. * | four null bytes |
  33. * | . . . |
  34. * | value 1 | ^
  35. * | value 3 | | growing upwards
  36. * | value 2 | |
  37. * +------------------+
  38. *
  39. * The header is followed by multiple entry descriptors. In disk blocks, the
  40. * entry descriptors are kept sorted. In inodes, they are unsorted. The
  41. * attribute values are aligned to the end of the block in no specific order.
  42. *
  43. * Locking strategy
  44. * ----------------
  45. * EXT4_I(inode)->i_file_acl is protected by EXT4_I(inode)->xattr_sem.
  46. * EA blocks are only changed if they are exclusive to an inode, so
  47. * holding xattr_sem also means that nothing but the EA block's reference
  48. * count can change. Multiple writers to the same block are synchronized
  49. * by the buffer lock.
  50. */
  51. #include <linux/init.h>
  52. #include <linux/fs.h>
  53. #include <linux/slab.h>
  54. #include <linux/mbcache.h>
  55. #include <linux/quotaops.h>
  56. #include "ext4_jbd2.h"
  57. #include "ext4.h"
  58. #include "xattr.h"
  59. #include "acl.h"
  60. #ifdef EXT4_XATTR_DEBUG
  61. # define ea_idebug(inode, f...) do { \
  62. printk(KERN_DEBUG "inode %s:%lu: ", \
  63. inode->i_sb->s_id, inode->i_ino); \
  64. printk(f); \
  65. printk("\n"); \
  66. } while (0)
  67. # define ea_bdebug(bh, f...) do { \
  68. char b[BDEVNAME_SIZE]; \
  69. printk(KERN_DEBUG "block %s:%lu: ", \
  70. bdevname(bh->b_bdev, b), \
  71. (unsigned long) bh->b_blocknr); \
  72. printk(f); \
  73. printk("\n"); \
  74. } while (0)
  75. #else
  76. # define ea_idebug(inode, fmt, ...) no_printk(fmt, ##__VA_ARGS__)
  77. # define ea_bdebug(bh, fmt, ...) no_printk(fmt, ##__VA_ARGS__)
  78. #endif
  79. static void ext4_xattr_cache_insert(struct mb_cache *, struct buffer_head *);
  80. static struct buffer_head *ext4_xattr_cache_find(struct inode *,
  81. struct ext4_xattr_header *,
  82. struct mb_cache_entry **);
  83. static void ext4_xattr_rehash(struct ext4_xattr_header *,
  84. struct ext4_xattr_entry *);
  85. static int ext4_xattr_list(struct dentry *dentry, char *buffer,
  86. size_t buffer_size);
  87. static const struct xattr_handler *ext4_xattr_handler_map[] = {
  88. [EXT4_XATTR_INDEX_USER] = &ext4_xattr_user_handler,
  89. #ifdef CONFIG_EXT4_FS_POSIX_ACL
  90. [EXT4_XATTR_INDEX_POSIX_ACL_ACCESS] = &posix_acl_access_xattr_handler,
  91. [EXT4_XATTR_INDEX_POSIX_ACL_DEFAULT] = &posix_acl_default_xattr_handler,
  92. #endif
  93. [EXT4_XATTR_INDEX_TRUSTED] = &ext4_xattr_trusted_handler,
  94. #ifdef CONFIG_EXT4_FS_SECURITY
  95. [EXT4_XATTR_INDEX_SECURITY] = &ext4_xattr_security_handler,
  96. #endif
  97. };
  98. const struct xattr_handler *ext4_xattr_handlers[] = {
  99. &ext4_xattr_user_handler,
  100. &ext4_xattr_trusted_handler,
  101. #ifdef CONFIG_EXT4_FS_POSIX_ACL
  102. &posix_acl_access_xattr_handler,
  103. &posix_acl_default_xattr_handler,
  104. #endif
  105. #ifdef CONFIG_EXT4_FS_SECURITY
  106. &ext4_xattr_security_handler,
  107. #endif
  108. NULL
  109. };
  110. #define EXT4_GET_MB_CACHE(inode) (((struct ext4_sb_info *) \
  111. inode->i_sb->s_fs_info)->s_mb_cache)
  112. static __le32 ext4_xattr_block_csum(struct inode *inode,
  113. sector_t block_nr,
  114. struct ext4_xattr_header *hdr)
  115. {
  116. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  117. __u32 csum;
  118. __le32 save_csum;
  119. __le64 dsk_block_nr = cpu_to_le64(block_nr);
  120. save_csum = hdr->h_checksum;
  121. hdr->h_checksum = 0;
  122. csum = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&dsk_block_nr,
  123. sizeof(dsk_block_nr));
  124. csum = ext4_chksum(sbi, csum, (__u8 *)hdr,
  125. EXT4_BLOCK_SIZE(inode->i_sb));
  126. hdr->h_checksum = save_csum;
  127. return cpu_to_le32(csum);
  128. }
  129. static int ext4_xattr_block_csum_verify(struct inode *inode,
  130. sector_t block_nr,
  131. struct ext4_xattr_header *hdr)
  132. {
  133. if (ext4_has_metadata_csum(inode->i_sb) &&
  134. (hdr->h_checksum != ext4_xattr_block_csum(inode, block_nr, hdr)))
  135. return 0;
  136. return 1;
  137. }
  138. static void ext4_xattr_block_csum_set(struct inode *inode,
  139. sector_t block_nr,
  140. struct ext4_xattr_header *hdr)
  141. {
  142. if (!ext4_has_metadata_csum(inode->i_sb))
  143. return;
  144. hdr->h_checksum = ext4_xattr_block_csum(inode, block_nr, hdr);
  145. }
  146. static inline int ext4_handle_dirty_xattr_block(handle_t *handle,
  147. struct inode *inode,
  148. struct buffer_head *bh)
  149. {
  150. ext4_xattr_block_csum_set(inode, bh->b_blocknr, BHDR(bh));
  151. return ext4_handle_dirty_metadata(handle, inode, bh);
  152. }
  153. static inline const struct xattr_handler *
  154. ext4_xattr_handler(int name_index)
  155. {
  156. const struct xattr_handler *handler = NULL;
  157. if (name_index > 0 && name_index < ARRAY_SIZE(ext4_xattr_handler_map))
  158. handler = ext4_xattr_handler_map[name_index];
  159. return handler;
  160. }
  161. /*
  162. * Inode operation listxattr()
  163. *
  164. * d_inode(dentry)->i_mutex: don't care
  165. */
  166. ssize_t
  167. ext4_listxattr(struct dentry *dentry, char *buffer, size_t size)
  168. {
  169. return ext4_xattr_list(dentry, buffer, size);
  170. }
  171. static int
  172. ext4_xattr_check_names(struct ext4_xattr_entry *entry, void *end,
  173. void *value_start)
  174. {
  175. struct ext4_xattr_entry *e = entry;
  176. while (!IS_LAST_ENTRY(e)) {
  177. struct ext4_xattr_entry *next = EXT4_XATTR_NEXT(e);
  178. if ((void *)next >= end)
  179. return -EFSCORRUPTED;
  180. e = next;
  181. }
  182. while (!IS_LAST_ENTRY(entry)) {
  183. if (entry->e_value_size != 0 &&
  184. (value_start + le16_to_cpu(entry->e_value_offs) <
  185. (void *)e + sizeof(__u32) ||
  186. value_start + le16_to_cpu(entry->e_value_offs) +
  187. le32_to_cpu(entry->e_value_size) > end))
  188. return -EFSCORRUPTED;
  189. entry = EXT4_XATTR_NEXT(entry);
  190. }
  191. return 0;
  192. }
  193. static inline int
  194. ext4_xattr_check_block(struct inode *inode, struct buffer_head *bh)
  195. {
  196. int error;
  197. if (buffer_verified(bh))
  198. return 0;
  199. if (BHDR(bh)->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC) ||
  200. BHDR(bh)->h_blocks != cpu_to_le32(1))
  201. return -EFSCORRUPTED;
  202. if (!ext4_xattr_block_csum_verify(inode, bh->b_blocknr, BHDR(bh)))
  203. return -EFSBADCRC;
  204. error = ext4_xattr_check_names(BFIRST(bh), bh->b_data + bh->b_size,
  205. bh->b_data);
  206. if (!error)
  207. set_buffer_verified(bh);
  208. return error;
  209. }
  210. static inline int
  211. ext4_xattr_check_entry(struct ext4_xattr_entry *entry, size_t size)
  212. {
  213. size_t value_size = le32_to_cpu(entry->e_value_size);
  214. if (entry->e_value_block != 0 || value_size > size ||
  215. le16_to_cpu(entry->e_value_offs) + value_size > size)
  216. return -EFSCORRUPTED;
  217. return 0;
  218. }
  219. static int
  220. ext4_xattr_find_entry(struct ext4_xattr_entry **pentry, int name_index,
  221. const char *name, size_t size, int sorted)
  222. {
  223. struct ext4_xattr_entry *entry;
  224. size_t name_len;
  225. int cmp = 1;
  226. if (name == NULL)
  227. return -EINVAL;
  228. name_len = strlen(name);
  229. entry = *pentry;
  230. for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
  231. cmp = name_index - entry->e_name_index;
  232. if (!cmp)
  233. cmp = name_len - entry->e_name_len;
  234. if (!cmp)
  235. cmp = memcmp(name, entry->e_name, name_len);
  236. if (cmp <= 0 && (sorted || cmp == 0))
  237. break;
  238. }
  239. *pentry = entry;
  240. if (!cmp && ext4_xattr_check_entry(entry, size))
  241. return -EFSCORRUPTED;
  242. return cmp ? -ENODATA : 0;
  243. }
  244. static int
  245. ext4_xattr_block_get(struct inode *inode, int name_index, const char *name,
  246. void *buffer, size_t buffer_size)
  247. {
  248. struct buffer_head *bh = NULL;
  249. struct ext4_xattr_entry *entry;
  250. size_t size;
  251. int error;
  252. struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
  253. ea_idebug(inode, "name=%d.%s, buffer=%p, buffer_size=%ld",
  254. name_index, name, buffer, (long)buffer_size);
  255. error = -ENODATA;
  256. if (!EXT4_I(inode)->i_file_acl)
  257. goto cleanup;
  258. ea_idebug(inode, "reading block %llu",
  259. (unsigned long long)EXT4_I(inode)->i_file_acl);
  260. bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
  261. if (!bh)
  262. goto cleanup;
  263. ea_bdebug(bh, "b_count=%d, refcount=%d",
  264. atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
  265. if (ext4_xattr_check_block(inode, bh)) {
  266. bad_block:
  267. EXT4_ERROR_INODE(inode, "bad block %llu",
  268. EXT4_I(inode)->i_file_acl);
  269. error = -EFSCORRUPTED;
  270. goto cleanup;
  271. }
  272. ext4_xattr_cache_insert(ext4_mb_cache, bh);
  273. entry = BFIRST(bh);
  274. error = ext4_xattr_find_entry(&entry, name_index, name, bh->b_size, 1);
  275. if (error == -EFSCORRUPTED)
  276. goto bad_block;
  277. if (error)
  278. goto cleanup;
  279. size = le32_to_cpu(entry->e_value_size);
  280. if (buffer) {
  281. error = -ERANGE;
  282. if (size > buffer_size)
  283. goto cleanup;
  284. memcpy(buffer, bh->b_data + le16_to_cpu(entry->e_value_offs),
  285. size);
  286. }
  287. error = size;
  288. cleanup:
  289. brelse(bh);
  290. return error;
  291. }
  292. int
  293. ext4_xattr_ibody_get(struct inode *inode, int name_index, const char *name,
  294. void *buffer, size_t buffer_size)
  295. {
  296. struct ext4_xattr_ibody_header *header;
  297. struct ext4_xattr_entry *entry;
  298. struct ext4_inode *raw_inode;
  299. struct ext4_iloc iloc;
  300. size_t size;
  301. void *end;
  302. int error;
  303. if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
  304. return -ENODATA;
  305. error = ext4_get_inode_loc(inode, &iloc);
  306. if (error)
  307. return error;
  308. raw_inode = ext4_raw_inode(&iloc);
  309. header = IHDR(inode, raw_inode);
  310. entry = IFIRST(header);
  311. end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
  312. error = ext4_xattr_check_names(entry, end, entry);
  313. if (error)
  314. goto cleanup;
  315. error = ext4_xattr_find_entry(&entry, name_index, name,
  316. end - (void *)entry, 0);
  317. if (error)
  318. goto cleanup;
  319. size = le32_to_cpu(entry->e_value_size);
  320. if (buffer) {
  321. error = -ERANGE;
  322. if (size > buffer_size)
  323. goto cleanup;
  324. memcpy(buffer, (void *)IFIRST(header) +
  325. le16_to_cpu(entry->e_value_offs), size);
  326. }
  327. error = size;
  328. cleanup:
  329. brelse(iloc.bh);
  330. return error;
  331. }
  332. /*
  333. * ext4_xattr_get()
  334. *
  335. * Copy an extended attribute into the buffer
  336. * provided, or compute the buffer size required.
  337. * Buffer is NULL to compute the size of the buffer required.
  338. *
  339. * Returns a negative error number on failure, or the number of bytes
  340. * used / required on success.
  341. */
  342. int
  343. ext4_xattr_get(struct inode *inode, int name_index, const char *name,
  344. void *buffer, size_t buffer_size)
  345. {
  346. int error;
  347. if (strlen(name) > 255)
  348. return -ERANGE;
  349. down_read(&EXT4_I(inode)->xattr_sem);
  350. error = ext4_xattr_ibody_get(inode, name_index, name, buffer,
  351. buffer_size);
  352. if (error == -ENODATA)
  353. error = ext4_xattr_block_get(inode, name_index, name, buffer,
  354. buffer_size);
  355. up_read(&EXT4_I(inode)->xattr_sem);
  356. return error;
  357. }
  358. static int
  359. ext4_xattr_list_entries(struct dentry *dentry, struct ext4_xattr_entry *entry,
  360. char *buffer, size_t buffer_size)
  361. {
  362. size_t rest = buffer_size;
  363. for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
  364. const struct xattr_handler *handler =
  365. ext4_xattr_handler(entry->e_name_index);
  366. if (handler) {
  367. size_t size = handler->list(handler, dentry, buffer,
  368. rest, entry->e_name,
  369. entry->e_name_len);
  370. if (buffer) {
  371. if (size > rest)
  372. return -ERANGE;
  373. buffer += size;
  374. }
  375. rest -= size;
  376. }
  377. }
  378. return buffer_size - rest;
  379. }
  380. static int
  381. ext4_xattr_block_list(struct dentry *dentry, char *buffer, size_t buffer_size)
  382. {
  383. struct inode *inode = d_inode(dentry);
  384. struct buffer_head *bh = NULL;
  385. int error;
  386. struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
  387. ea_idebug(inode, "buffer=%p, buffer_size=%ld",
  388. buffer, (long)buffer_size);
  389. error = 0;
  390. if (!EXT4_I(inode)->i_file_acl)
  391. goto cleanup;
  392. ea_idebug(inode, "reading block %llu",
  393. (unsigned long long)EXT4_I(inode)->i_file_acl);
  394. bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
  395. error = -EIO;
  396. if (!bh)
  397. goto cleanup;
  398. ea_bdebug(bh, "b_count=%d, refcount=%d",
  399. atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
  400. if (ext4_xattr_check_block(inode, bh)) {
  401. EXT4_ERROR_INODE(inode, "bad block %llu",
  402. EXT4_I(inode)->i_file_acl);
  403. error = -EFSCORRUPTED;
  404. goto cleanup;
  405. }
  406. ext4_xattr_cache_insert(ext4_mb_cache, bh);
  407. error = ext4_xattr_list_entries(dentry, BFIRST(bh), buffer, buffer_size);
  408. cleanup:
  409. brelse(bh);
  410. return error;
  411. }
  412. static int
  413. ext4_xattr_ibody_list(struct dentry *dentry, char *buffer, size_t buffer_size)
  414. {
  415. struct inode *inode = d_inode(dentry);
  416. struct ext4_xattr_ibody_header *header;
  417. struct ext4_inode *raw_inode;
  418. struct ext4_iloc iloc;
  419. void *end;
  420. int error;
  421. if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
  422. return 0;
  423. error = ext4_get_inode_loc(inode, &iloc);
  424. if (error)
  425. return error;
  426. raw_inode = ext4_raw_inode(&iloc);
  427. header = IHDR(inode, raw_inode);
  428. end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
  429. error = ext4_xattr_check_names(IFIRST(header), end, IFIRST(header));
  430. if (error)
  431. goto cleanup;
  432. error = ext4_xattr_list_entries(dentry, IFIRST(header),
  433. buffer, buffer_size);
  434. cleanup:
  435. brelse(iloc.bh);
  436. return error;
  437. }
  438. /*
  439. * ext4_xattr_list()
  440. *
  441. * Copy a list of attribute names into the buffer
  442. * provided, or compute the buffer size required.
  443. * Buffer is NULL to compute the size of the buffer required.
  444. *
  445. * Returns a negative error number on failure, or the number of bytes
  446. * used / required on success.
  447. */
  448. static int
  449. ext4_xattr_list(struct dentry *dentry, char *buffer, size_t buffer_size)
  450. {
  451. int ret, ret2;
  452. down_read(&EXT4_I(d_inode(dentry))->xattr_sem);
  453. ret = ret2 = ext4_xattr_ibody_list(dentry, buffer, buffer_size);
  454. if (ret < 0)
  455. goto errout;
  456. if (buffer) {
  457. buffer += ret;
  458. buffer_size -= ret;
  459. }
  460. ret = ext4_xattr_block_list(dentry, buffer, buffer_size);
  461. if (ret < 0)
  462. goto errout;
  463. ret += ret2;
  464. errout:
  465. up_read(&EXT4_I(d_inode(dentry))->xattr_sem);
  466. return ret;
  467. }
  468. /*
  469. * If the EXT4_FEATURE_COMPAT_EXT_ATTR feature of this file system is
  470. * not set, set it.
  471. */
  472. static void ext4_xattr_update_super_block(handle_t *handle,
  473. struct super_block *sb)
  474. {
  475. if (ext4_has_feature_xattr(sb))
  476. return;
  477. BUFFER_TRACE(EXT4_SB(sb)->s_sbh, "get_write_access");
  478. if (ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh) == 0) {
  479. ext4_set_feature_xattr(sb);
  480. ext4_handle_dirty_super(handle, sb);
  481. }
  482. }
  483. /*
  484. * Release the xattr block BH: If the reference count is > 1, decrement it;
  485. * otherwise free the block.
  486. */
  487. static void
  488. ext4_xattr_release_block(handle_t *handle, struct inode *inode,
  489. struct buffer_head *bh)
  490. {
  491. struct mb_cache_entry *ce = NULL;
  492. int error = 0;
  493. struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
  494. ce = mb_cache_entry_get(ext4_mb_cache, bh->b_bdev, bh->b_blocknr);
  495. BUFFER_TRACE(bh, "get_write_access");
  496. error = ext4_journal_get_write_access(handle, bh);
  497. if (error)
  498. goto out;
  499. lock_buffer(bh);
  500. if (BHDR(bh)->h_refcount == cpu_to_le32(1)) {
  501. ea_bdebug(bh, "refcount now=0; freeing");
  502. if (ce)
  503. mb_cache_entry_free(ce);
  504. get_bh(bh);
  505. unlock_buffer(bh);
  506. ext4_free_blocks(handle, inode, bh, 0, 1,
  507. EXT4_FREE_BLOCKS_METADATA |
  508. EXT4_FREE_BLOCKS_FORGET);
  509. } else {
  510. le32_add_cpu(&BHDR(bh)->h_refcount, -1);
  511. if (ce)
  512. mb_cache_entry_release(ce);
  513. /*
  514. * Beware of this ugliness: Releasing of xattr block references
  515. * from different inodes can race and so we have to protect
  516. * from a race where someone else frees the block (and releases
  517. * its journal_head) before we are done dirtying the buffer. In
  518. * nojournal mode this race is harmless and we actually cannot
  519. * call ext4_handle_dirty_xattr_block() with locked buffer as
  520. * that function can call sync_dirty_buffer() so for that case
  521. * we handle the dirtying after unlocking the buffer.
  522. */
  523. if (ext4_handle_valid(handle))
  524. error = ext4_handle_dirty_xattr_block(handle, inode,
  525. bh);
  526. unlock_buffer(bh);
  527. if (!ext4_handle_valid(handle))
  528. error = ext4_handle_dirty_xattr_block(handle, inode,
  529. bh);
  530. if (IS_SYNC(inode))
  531. ext4_handle_sync(handle);
  532. dquot_free_block(inode, EXT4_C2B(EXT4_SB(inode->i_sb), 1));
  533. ea_bdebug(bh, "refcount now=%d; releasing",
  534. le32_to_cpu(BHDR(bh)->h_refcount));
  535. }
  536. out:
  537. ext4_std_error(inode->i_sb, error);
  538. return;
  539. }
  540. /*
  541. * Find the available free space for EAs. This also returns the total number of
  542. * bytes used by EA entries.
  543. */
  544. static size_t ext4_xattr_free_space(struct ext4_xattr_entry *last,
  545. size_t *min_offs, void *base, int *total)
  546. {
  547. for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
  548. if (!last->e_value_block && last->e_value_size) {
  549. size_t offs = le16_to_cpu(last->e_value_offs);
  550. if (offs < *min_offs)
  551. *min_offs = offs;
  552. }
  553. if (total)
  554. *total += EXT4_XATTR_LEN(last->e_name_len);
  555. }
  556. return (*min_offs - ((void *)last - base) - sizeof(__u32));
  557. }
  558. static int
  559. ext4_xattr_set_entry(struct ext4_xattr_info *i, struct ext4_xattr_search *s)
  560. {
  561. struct ext4_xattr_entry *last;
  562. size_t free, min_offs = s->end - s->base, name_len = strlen(i->name);
  563. /* Compute min_offs and last. */
  564. last = s->first;
  565. for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
  566. if (!last->e_value_block && last->e_value_size) {
  567. size_t offs = le16_to_cpu(last->e_value_offs);
  568. if (offs < min_offs)
  569. min_offs = offs;
  570. }
  571. }
  572. free = min_offs - ((void *)last - s->base) - sizeof(__u32);
  573. if (!s->not_found) {
  574. if (!s->here->e_value_block && s->here->e_value_size) {
  575. size_t size = le32_to_cpu(s->here->e_value_size);
  576. free += EXT4_XATTR_SIZE(size);
  577. }
  578. free += EXT4_XATTR_LEN(name_len);
  579. }
  580. if (i->value) {
  581. if (free < EXT4_XATTR_LEN(name_len) +
  582. EXT4_XATTR_SIZE(i->value_len))
  583. return -ENOSPC;
  584. }
  585. if (i->value && s->not_found) {
  586. /* Insert the new name. */
  587. size_t size = EXT4_XATTR_LEN(name_len);
  588. size_t rest = (void *)last - (void *)s->here + sizeof(__u32);
  589. memmove((void *)s->here + size, s->here, rest);
  590. memset(s->here, 0, size);
  591. s->here->e_name_index = i->name_index;
  592. s->here->e_name_len = name_len;
  593. memcpy(s->here->e_name, i->name, name_len);
  594. } else {
  595. if (!s->here->e_value_block && s->here->e_value_size) {
  596. void *first_val = s->base + min_offs;
  597. size_t offs = le16_to_cpu(s->here->e_value_offs);
  598. void *val = s->base + offs;
  599. size_t size = EXT4_XATTR_SIZE(
  600. le32_to_cpu(s->here->e_value_size));
  601. if (i->value && size == EXT4_XATTR_SIZE(i->value_len)) {
  602. /* The old and the new value have the same
  603. size. Just replace. */
  604. s->here->e_value_size =
  605. cpu_to_le32(i->value_len);
  606. if (i->value == EXT4_ZERO_XATTR_VALUE) {
  607. memset(val, 0, size);
  608. } else {
  609. /* Clear pad bytes first. */
  610. memset(val + size - EXT4_XATTR_PAD, 0,
  611. EXT4_XATTR_PAD);
  612. memcpy(val, i->value, i->value_len);
  613. }
  614. return 0;
  615. }
  616. /* Remove the old value. */
  617. memmove(first_val + size, first_val, val - first_val);
  618. memset(first_val, 0, size);
  619. s->here->e_value_size = 0;
  620. s->here->e_value_offs = 0;
  621. min_offs += size;
  622. /* Adjust all value offsets. */
  623. last = s->first;
  624. while (!IS_LAST_ENTRY(last)) {
  625. size_t o = le16_to_cpu(last->e_value_offs);
  626. if (!last->e_value_block &&
  627. last->e_value_size && o < offs)
  628. last->e_value_offs =
  629. cpu_to_le16(o + size);
  630. last = EXT4_XATTR_NEXT(last);
  631. }
  632. }
  633. if (!i->value) {
  634. /* Remove the old name. */
  635. size_t size = EXT4_XATTR_LEN(name_len);
  636. last = ENTRY((void *)last - size);
  637. memmove(s->here, (void *)s->here + size,
  638. (void *)last - (void *)s->here + sizeof(__u32));
  639. memset(last, 0, size);
  640. }
  641. }
  642. if (i->value) {
  643. /* Insert the new value. */
  644. s->here->e_value_size = cpu_to_le32(i->value_len);
  645. if (i->value_len) {
  646. size_t size = EXT4_XATTR_SIZE(i->value_len);
  647. void *val = s->base + min_offs - size;
  648. s->here->e_value_offs = cpu_to_le16(min_offs - size);
  649. if (i->value == EXT4_ZERO_XATTR_VALUE) {
  650. memset(val, 0, size);
  651. } else {
  652. /* Clear the pad bytes first. */
  653. memset(val + size - EXT4_XATTR_PAD, 0,
  654. EXT4_XATTR_PAD);
  655. memcpy(val, i->value, i->value_len);
  656. }
  657. }
  658. }
  659. return 0;
  660. }
  661. struct ext4_xattr_block_find {
  662. struct ext4_xattr_search s;
  663. struct buffer_head *bh;
  664. };
  665. static int
  666. ext4_xattr_block_find(struct inode *inode, struct ext4_xattr_info *i,
  667. struct ext4_xattr_block_find *bs)
  668. {
  669. struct super_block *sb = inode->i_sb;
  670. int error;
  671. ea_idebug(inode, "name=%d.%s, value=%p, value_len=%ld",
  672. i->name_index, i->name, i->value, (long)i->value_len);
  673. if (EXT4_I(inode)->i_file_acl) {
  674. /* The inode already has an extended attribute block. */
  675. bs->bh = sb_bread(sb, EXT4_I(inode)->i_file_acl);
  676. error = -EIO;
  677. if (!bs->bh)
  678. goto cleanup;
  679. ea_bdebug(bs->bh, "b_count=%d, refcount=%d",
  680. atomic_read(&(bs->bh->b_count)),
  681. le32_to_cpu(BHDR(bs->bh)->h_refcount));
  682. if (ext4_xattr_check_block(inode, bs->bh)) {
  683. EXT4_ERROR_INODE(inode, "bad block %llu",
  684. EXT4_I(inode)->i_file_acl);
  685. error = -EFSCORRUPTED;
  686. goto cleanup;
  687. }
  688. /* Find the named attribute. */
  689. bs->s.base = BHDR(bs->bh);
  690. bs->s.first = BFIRST(bs->bh);
  691. bs->s.end = bs->bh->b_data + bs->bh->b_size;
  692. bs->s.here = bs->s.first;
  693. error = ext4_xattr_find_entry(&bs->s.here, i->name_index,
  694. i->name, bs->bh->b_size, 1);
  695. if (error && error != -ENODATA)
  696. goto cleanup;
  697. bs->s.not_found = error;
  698. }
  699. error = 0;
  700. cleanup:
  701. return error;
  702. }
  703. static int
  704. ext4_xattr_block_set(handle_t *handle, struct inode *inode,
  705. struct ext4_xattr_info *i,
  706. struct ext4_xattr_block_find *bs)
  707. {
  708. struct super_block *sb = inode->i_sb;
  709. struct buffer_head *new_bh = NULL;
  710. struct ext4_xattr_search *s = &bs->s;
  711. struct mb_cache_entry *ce = NULL;
  712. int error = 0;
  713. struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
  714. #define header(x) ((struct ext4_xattr_header *)(x))
  715. if (i->value && i->value_len > sb->s_blocksize)
  716. return -ENOSPC;
  717. if (s->base) {
  718. ce = mb_cache_entry_get(ext4_mb_cache, bs->bh->b_bdev,
  719. bs->bh->b_blocknr);
  720. BUFFER_TRACE(bs->bh, "get_write_access");
  721. error = ext4_journal_get_write_access(handle, bs->bh);
  722. if (error)
  723. goto cleanup;
  724. lock_buffer(bs->bh);
  725. if (header(s->base)->h_refcount == cpu_to_le32(1)) {
  726. if (ce) {
  727. mb_cache_entry_free(ce);
  728. ce = NULL;
  729. }
  730. ea_bdebug(bs->bh, "modifying in-place");
  731. error = ext4_xattr_set_entry(i, s);
  732. if (!error) {
  733. if (!IS_LAST_ENTRY(s->first))
  734. ext4_xattr_rehash(header(s->base),
  735. s->here);
  736. ext4_xattr_cache_insert(ext4_mb_cache,
  737. bs->bh);
  738. }
  739. unlock_buffer(bs->bh);
  740. if (error == -EFSCORRUPTED)
  741. goto bad_block;
  742. if (!error)
  743. error = ext4_handle_dirty_xattr_block(handle,
  744. inode,
  745. bs->bh);
  746. if (error)
  747. goto cleanup;
  748. goto inserted;
  749. } else {
  750. int offset = (char *)s->here - bs->bh->b_data;
  751. unlock_buffer(bs->bh);
  752. if (ce) {
  753. mb_cache_entry_release(ce);
  754. ce = NULL;
  755. }
  756. ea_bdebug(bs->bh, "cloning");
  757. s->base = kmalloc(bs->bh->b_size, GFP_NOFS);
  758. error = -ENOMEM;
  759. if (s->base == NULL)
  760. goto cleanup;
  761. memcpy(s->base, BHDR(bs->bh), bs->bh->b_size);
  762. s->first = ENTRY(header(s->base)+1);
  763. header(s->base)->h_refcount = cpu_to_le32(1);
  764. s->here = ENTRY(s->base + offset);
  765. s->end = s->base + bs->bh->b_size;
  766. }
  767. } else {
  768. /* Allocate a buffer where we construct the new block. */
  769. s->base = kzalloc(sb->s_blocksize, GFP_NOFS);
  770. /* assert(header == s->base) */
  771. error = -ENOMEM;
  772. if (s->base == NULL)
  773. goto cleanup;
  774. header(s->base)->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
  775. header(s->base)->h_blocks = cpu_to_le32(1);
  776. header(s->base)->h_refcount = cpu_to_le32(1);
  777. s->first = ENTRY(header(s->base)+1);
  778. s->here = ENTRY(header(s->base)+1);
  779. s->end = s->base + sb->s_blocksize;
  780. }
  781. error = ext4_xattr_set_entry(i, s);
  782. if (error == -EFSCORRUPTED)
  783. goto bad_block;
  784. if (error)
  785. goto cleanup;
  786. if (!IS_LAST_ENTRY(s->first))
  787. ext4_xattr_rehash(header(s->base), s->here);
  788. inserted:
  789. if (!IS_LAST_ENTRY(s->first)) {
  790. new_bh = ext4_xattr_cache_find(inode, header(s->base), &ce);
  791. if (new_bh) {
  792. /* We found an identical block in the cache. */
  793. if (new_bh == bs->bh)
  794. ea_bdebug(new_bh, "keeping");
  795. else {
  796. /* The old block is released after updating
  797. the inode. */
  798. error = dquot_alloc_block(inode,
  799. EXT4_C2B(EXT4_SB(sb), 1));
  800. if (error)
  801. goto cleanup;
  802. BUFFER_TRACE(new_bh, "get_write_access");
  803. error = ext4_journal_get_write_access(handle,
  804. new_bh);
  805. if (error)
  806. goto cleanup_dquot;
  807. lock_buffer(new_bh);
  808. le32_add_cpu(&BHDR(new_bh)->h_refcount, 1);
  809. ea_bdebug(new_bh, "reusing; refcount now=%d",
  810. le32_to_cpu(BHDR(new_bh)->h_refcount));
  811. unlock_buffer(new_bh);
  812. error = ext4_handle_dirty_xattr_block(handle,
  813. inode,
  814. new_bh);
  815. if (error)
  816. goto cleanup_dquot;
  817. }
  818. mb_cache_entry_release(ce);
  819. ce = NULL;
  820. } else if (bs->bh && s->base == bs->bh->b_data) {
  821. /* We were modifying this block in-place. */
  822. ea_bdebug(bs->bh, "keeping this block");
  823. new_bh = bs->bh;
  824. get_bh(new_bh);
  825. } else {
  826. /* We need to allocate a new block */
  827. ext4_fsblk_t goal, block;
  828. goal = ext4_group_first_block_no(sb,
  829. EXT4_I(inode)->i_block_group);
  830. /* non-extent files can't have physical blocks past 2^32 */
  831. if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
  832. goal = goal & EXT4_MAX_BLOCK_FILE_PHYS;
  833. block = ext4_new_meta_blocks(handle, inode, goal, 0,
  834. NULL, &error);
  835. if (error)
  836. goto cleanup;
  837. if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
  838. BUG_ON(block > EXT4_MAX_BLOCK_FILE_PHYS);
  839. ea_idebug(inode, "creating block %llu",
  840. (unsigned long long)block);
  841. new_bh = sb_getblk(sb, block);
  842. if (unlikely(!new_bh)) {
  843. error = -ENOMEM;
  844. getblk_failed:
  845. ext4_free_blocks(handle, inode, NULL, block, 1,
  846. EXT4_FREE_BLOCKS_METADATA);
  847. goto cleanup;
  848. }
  849. lock_buffer(new_bh);
  850. error = ext4_journal_get_create_access(handle, new_bh);
  851. if (error) {
  852. unlock_buffer(new_bh);
  853. error = -EIO;
  854. goto getblk_failed;
  855. }
  856. memcpy(new_bh->b_data, s->base, new_bh->b_size);
  857. set_buffer_uptodate(new_bh);
  858. unlock_buffer(new_bh);
  859. ext4_xattr_cache_insert(ext4_mb_cache, new_bh);
  860. error = ext4_handle_dirty_xattr_block(handle,
  861. inode, new_bh);
  862. if (error)
  863. goto cleanup;
  864. }
  865. }
  866. /* Update the inode. */
  867. EXT4_I(inode)->i_file_acl = new_bh ? new_bh->b_blocknr : 0;
  868. /* Drop the previous xattr block. */
  869. if (bs->bh && bs->bh != new_bh)
  870. ext4_xattr_release_block(handle, inode, bs->bh);
  871. error = 0;
  872. cleanup:
  873. if (ce)
  874. mb_cache_entry_release(ce);
  875. brelse(new_bh);
  876. if (!(bs->bh && s->base == bs->bh->b_data))
  877. kfree(s->base);
  878. return error;
  879. cleanup_dquot:
  880. dquot_free_block(inode, EXT4_C2B(EXT4_SB(sb), 1));
  881. goto cleanup;
  882. bad_block:
  883. EXT4_ERROR_INODE(inode, "bad block %llu",
  884. EXT4_I(inode)->i_file_acl);
  885. goto cleanup;
  886. #undef header
  887. }
  888. int ext4_xattr_ibody_find(struct inode *inode, struct ext4_xattr_info *i,
  889. struct ext4_xattr_ibody_find *is)
  890. {
  891. struct ext4_xattr_ibody_header *header;
  892. struct ext4_inode *raw_inode;
  893. int error;
  894. if (EXT4_I(inode)->i_extra_isize == 0)
  895. return 0;
  896. raw_inode = ext4_raw_inode(&is->iloc);
  897. header = IHDR(inode, raw_inode);
  898. is->s.base = is->s.first = IFIRST(header);
  899. is->s.here = is->s.first;
  900. is->s.end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
  901. if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
  902. error = ext4_xattr_check_names(IFIRST(header), is->s.end,
  903. IFIRST(header));
  904. if (error)
  905. return error;
  906. /* Find the named attribute. */
  907. error = ext4_xattr_find_entry(&is->s.here, i->name_index,
  908. i->name, is->s.end -
  909. (void *)is->s.base, 0);
  910. if (error && error != -ENODATA)
  911. return error;
  912. is->s.not_found = error;
  913. }
  914. return 0;
  915. }
  916. int ext4_xattr_ibody_inline_set(handle_t *handle, struct inode *inode,
  917. struct ext4_xattr_info *i,
  918. struct ext4_xattr_ibody_find *is)
  919. {
  920. struct ext4_xattr_ibody_header *header;
  921. struct ext4_xattr_search *s = &is->s;
  922. int error;
  923. if (EXT4_I(inode)->i_extra_isize == 0)
  924. return -ENOSPC;
  925. error = ext4_xattr_set_entry(i, s);
  926. if (error) {
  927. if (error == -ENOSPC &&
  928. ext4_has_inline_data(inode)) {
  929. error = ext4_try_to_evict_inline_data(handle, inode,
  930. EXT4_XATTR_LEN(strlen(i->name) +
  931. EXT4_XATTR_SIZE(i->value_len)));
  932. if (error)
  933. return error;
  934. error = ext4_xattr_ibody_find(inode, i, is);
  935. if (error)
  936. return error;
  937. error = ext4_xattr_set_entry(i, s);
  938. }
  939. if (error)
  940. return error;
  941. }
  942. header = IHDR(inode, ext4_raw_inode(&is->iloc));
  943. if (!IS_LAST_ENTRY(s->first)) {
  944. header->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
  945. ext4_set_inode_state(inode, EXT4_STATE_XATTR);
  946. } else {
  947. header->h_magic = cpu_to_le32(0);
  948. ext4_clear_inode_state(inode, EXT4_STATE_XATTR);
  949. }
  950. return 0;
  951. }
  952. static int ext4_xattr_ibody_set(handle_t *handle, struct inode *inode,
  953. struct ext4_xattr_info *i,
  954. struct ext4_xattr_ibody_find *is)
  955. {
  956. struct ext4_xattr_ibody_header *header;
  957. struct ext4_xattr_search *s = &is->s;
  958. int error;
  959. if (EXT4_I(inode)->i_extra_isize == 0)
  960. return -ENOSPC;
  961. error = ext4_xattr_set_entry(i, s);
  962. if (error)
  963. return error;
  964. header = IHDR(inode, ext4_raw_inode(&is->iloc));
  965. if (!IS_LAST_ENTRY(s->first)) {
  966. header->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
  967. ext4_set_inode_state(inode, EXT4_STATE_XATTR);
  968. } else {
  969. header->h_magic = cpu_to_le32(0);
  970. ext4_clear_inode_state(inode, EXT4_STATE_XATTR);
  971. }
  972. return 0;
  973. }
  974. /*
  975. * ext4_xattr_set_handle()
  976. *
  977. * Create, replace or remove an extended attribute for this inode. Value
  978. * is NULL to remove an existing extended attribute, and non-NULL to
  979. * either replace an existing extended attribute, or create a new extended
  980. * attribute. The flags XATTR_REPLACE and XATTR_CREATE
  981. * specify that an extended attribute must exist and must not exist
  982. * previous to the call, respectively.
  983. *
  984. * Returns 0, or a negative error number on failure.
  985. */
  986. int
  987. ext4_xattr_set_handle(handle_t *handle, struct inode *inode, int name_index,
  988. const char *name, const void *value, size_t value_len,
  989. int flags)
  990. {
  991. struct ext4_xattr_info i = {
  992. .name_index = name_index,
  993. .name = name,
  994. .value = value,
  995. .value_len = value_len,
  996. };
  997. struct ext4_xattr_ibody_find is = {
  998. .s = { .not_found = -ENODATA, },
  999. };
  1000. struct ext4_xattr_block_find bs = {
  1001. .s = { .not_found = -ENODATA, },
  1002. };
  1003. unsigned long no_expand;
  1004. int error;
  1005. if (!name)
  1006. return -EINVAL;
  1007. if (strlen(name) > 255)
  1008. return -ERANGE;
  1009. down_write(&EXT4_I(inode)->xattr_sem);
  1010. no_expand = ext4_test_inode_state(inode, EXT4_STATE_NO_EXPAND);
  1011. ext4_set_inode_state(inode, EXT4_STATE_NO_EXPAND);
  1012. error = ext4_reserve_inode_write(handle, inode, &is.iloc);
  1013. if (error)
  1014. goto cleanup;
  1015. if (ext4_test_inode_state(inode, EXT4_STATE_NEW)) {
  1016. struct ext4_inode *raw_inode = ext4_raw_inode(&is.iloc);
  1017. memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size);
  1018. ext4_clear_inode_state(inode, EXT4_STATE_NEW);
  1019. }
  1020. error = ext4_xattr_ibody_find(inode, &i, &is);
  1021. if (error)
  1022. goto cleanup;
  1023. if (is.s.not_found)
  1024. error = ext4_xattr_block_find(inode, &i, &bs);
  1025. if (error)
  1026. goto cleanup;
  1027. if (is.s.not_found && bs.s.not_found) {
  1028. error = -ENODATA;
  1029. if (flags & XATTR_REPLACE)
  1030. goto cleanup;
  1031. error = 0;
  1032. if (!value)
  1033. goto cleanup;
  1034. } else {
  1035. error = -EEXIST;
  1036. if (flags & XATTR_CREATE)
  1037. goto cleanup;
  1038. }
  1039. if (!value) {
  1040. if (!is.s.not_found)
  1041. error = ext4_xattr_ibody_set(handle, inode, &i, &is);
  1042. else if (!bs.s.not_found)
  1043. error = ext4_xattr_block_set(handle, inode, &i, &bs);
  1044. } else {
  1045. error = ext4_xattr_ibody_set(handle, inode, &i, &is);
  1046. if (!error && !bs.s.not_found) {
  1047. i.value = NULL;
  1048. error = ext4_xattr_block_set(handle, inode, &i, &bs);
  1049. } else if (error == -ENOSPC) {
  1050. if (EXT4_I(inode)->i_file_acl && !bs.s.base) {
  1051. error = ext4_xattr_block_find(inode, &i, &bs);
  1052. if (error)
  1053. goto cleanup;
  1054. }
  1055. error = ext4_xattr_block_set(handle, inode, &i, &bs);
  1056. if (error)
  1057. goto cleanup;
  1058. if (!is.s.not_found) {
  1059. i.value = NULL;
  1060. error = ext4_xattr_ibody_set(handle, inode, &i,
  1061. &is);
  1062. }
  1063. }
  1064. }
  1065. if (!error) {
  1066. ext4_xattr_update_super_block(handle, inode->i_sb);
  1067. inode->i_ctime = ext4_current_time(inode);
  1068. if (!value)
  1069. ext4_clear_inode_state(inode, EXT4_STATE_NO_EXPAND);
  1070. error = ext4_mark_iloc_dirty(handle, inode, &is.iloc);
  1071. /*
  1072. * The bh is consumed by ext4_mark_iloc_dirty, even with
  1073. * error != 0.
  1074. */
  1075. is.iloc.bh = NULL;
  1076. if (IS_SYNC(inode))
  1077. ext4_handle_sync(handle);
  1078. }
  1079. cleanup:
  1080. brelse(is.iloc.bh);
  1081. brelse(bs.bh);
  1082. if (no_expand == 0)
  1083. ext4_clear_inode_state(inode, EXT4_STATE_NO_EXPAND);
  1084. up_write(&EXT4_I(inode)->xattr_sem);
  1085. return error;
  1086. }
  1087. /*
  1088. * ext4_xattr_set()
  1089. *
  1090. * Like ext4_xattr_set_handle, but start from an inode. This extended
  1091. * attribute modification is a filesystem transaction by itself.
  1092. *
  1093. * Returns 0, or a negative error number on failure.
  1094. */
  1095. int
  1096. ext4_xattr_set(struct inode *inode, int name_index, const char *name,
  1097. const void *value, size_t value_len, int flags)
  1098. {
  1099. handle_t *handle;
  1100. int error, retries = 0;
  1101. int credits = ext4_jbd2_credits_xattr(inode);
  1102. retry:
  1103. handle = ext4_journal_start(inode, EXT4_HT_XATTR, credits);
  1104. if (IS_ERR(handle)) {
  1105. error = PTR_ERR(handle);
  1106. } else {
  1107. int error2;
  1108. error = ext4_xattr_set_handle(handle, inode, name_index, name,
  1109. value, value_len, flags);
  1110. error2 = ext4_journal_stop(handle);
  1111. if (error == -ENOSPC &&
  1112. ext4_should_retry_alloc(inode->i_sb, &retries))
  1113. goto retry;
  1114. if (error == 0)
  1115. error = error2;
  1116. }
  1117. return error;
  1118. }
  1119. /*
  1120. * Shift the EA entries in the inode to create space for the increased
  1121. * i_extra_isize.
  1122. */
  1123. static void ext4_xattr_shift_entries(struct ext4_xattr_entry *entry,
  1124. int value_offs_shift, void *to,
  1125. void *from, size_t n, int blocksize)
  1126. {
  1127. struct ext4_xattr_entry *last = entry;
  1128. int new_offs;
  1129. /* Adjust the value offsets of the entries */
  1130. for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
  1131. if (!last->e_value_block && last->e_value_size) {
  1132. new_offs = le16_to_cpu(last->e_value_offs) +
  1133. value_offs_shift;
  1134. BUG_ON(new_offs + le32_to_cpu(last->e_value_size)
  1135. > blocksize);
  1136. last->e_value_offs = cpu_to_le16(new_offs);
  1137. }
  1138. }
  1139. /* Shift the entries by n bytes */
  1140. memmove(to, from, n);
  1141. }
  1142. /*
  1143. * Expand an inode by new_extra_isize bytes when EAs are present.
  1144. * Returns 0 on success or negative error number on failure.
  1145. */
  1146. int ext4_expand_extra_isize_ea(struct inode *inode, int new_extra_isize,
  1147. struct ext4_inode *raw_inode, handle_t *handle)
  1148. {
  1149. struct ext4_xattr_ibody_header *header;
  1150. struct ext4_xattr_entry *entry, *last, *first;
  1151. struct buffer_head *bh = NULL;
  1152. struct ext4_xattr_ibody_find *is = NULL;
  1153. struct ext4_xattr_block_find *bs = NULL;
  1154. char *buffer = NULL, *b_entry_name = NULL;
  1155. size_t min_offs, free;
  1156. int total_ino;
  1157. void *base, *start, *end;
  1158. int extra_isize = 0, error = 0, tried_min_extra_isize = 0;
  1159. int s_min_extra_isize = le16_to_cpu(EXT4_SB(inode->i_sb)->s_es->s_min_extra_isize);
  1160. down_write(&EXT4_I(inode)->xattr_sem);
  1161. retry:
  1162. if (EXT4_I(inode)->i_extra_isize >= new_extra_isize) {
  1163. up_write(&EXT4_I(inode)->xattr_sem);
  1164. return 0;
  1165. }
  1166. header = IHDR(inode, raw_inode);
  1167. entry = IFIRST(header);
  1168. /*
  1169. * Check if enough free space is available in the inode to shift the
  1170. * entries ahead by new_extra_isize.
  1171. */
  1172. base = start = entry;
  1173. end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
  1174. min_offs = end - base;
  1175. last = entry;
  1176. total_ino = sizeof(struct ext4_xattr_ibody_header);
  1177. free = ext4_xattr_free_space(last, &min_offs, base, &total_ino);
  1178. if (free >= new_extra_isize) {
  1179. entry = IFIRST(header);
  1180. ext4_xattr_shift_entries(entry, EXT4_I(inode)->i_extra_isize
  1181. - new_extra_isize, (void *)raw_inode +
  1182. EXT4_GOOD_OLD_INODE_SIZE + new_extra_isize,
  1183. (void *)header, total_ino,
  1184. inode->i_sb->s_blocksize);
  1185. EXT4_I(inode)->i_extra_isize = new_extra_isize;
  1186. error = 0;
  1187. goto cleanup;
  1188. }
  1189. /*
  1190. * Enough free space isn't available in the inode, check if
  1191. * EA block can hold new_extra_isize bytes.
  1192. */
  1193. if (EXT4_I(inode)->i_file_acl) {
  1194. bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
  1195. error = -EIO;
  1196. if (!bh)
  1197. goto cleanup;
  1198. if (ext4_xattr_check_block(inode, bh)) {
  1199. EXT4_ERROR_INODE(inode, "bad block %llu",
  1200. EXT4_I(inode)->i_file_acl);
  1201. error = -EFSCORRUPTED;
  1202. goto cleanup;
  1203. }
  1204. base = BHDR(bh);
  1205. first = BFIRST(bh);
  1206. end = bh->b_data + bh->b_size;
  1207. min_offs = end - base;
  1208. free = ext4_xattr_free_space(first, &min_offs, base, NULL);
  1209. if (free < new_extra_isize) {
  1210. if (!tried_min_extra_isize && s_min_extra_isize) {
  1211. tried_min_extra_isize++;
  1212. new_extra_isize = s_min_extra_isize;
  1213. brelse(bh);
  1214. goto retry;
  1215. }
  1216. error = -1;
  1217. goto cleanup;
  1218. }
  1219. } else {
  1220. free = inode->i_sb->s_blocksize;
  1221. }
  1222. while (new_extra_isize > 0) {
  1223. size_t offs, size, entry_size;
  1224. struct ext4_xattr_entry *small_entry = NULL;
  1225. struct ext4_xattr_info i = {
  1226. .value = NULL,
  1227. .value_len = 0,
  1228. };
  1229. unsigned int total_size; /* EA entry size + value size */
  1230. unsigned int shift_bytes; /* No. of bytes to shift EAs by? */
  1231. unsigned int min_total_size = ~0U;
  1232. is = kzalloc(sizeof(struct ext4_xattr_ibody_find), GFP_NOFS);
  1233. bs = kzalloc(sizeof(struct ext4_xattr_block_find), GFP_NOFS);
  1234. if (!is || !bs) {
  1235. error = -ENOMEM;
  1236. goto cleanup;
  1237. }
  1238. is->s.not_found = -ENODATA;
  1239. bs->s.not_found = -ENODATA;
  1240. is->iloc.bh = NULL;
  1241. bs->bh = NULL;
  1242. last = IFIRST(header);
  1243. /* Find the entry best suited to be pushed into EA block */
  1244. entry = NULL;
  1245. for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
  1246. total_size =
  1247. EXT4_XATTR_SIZE(le32_to_cpu(last->e_value_size)) +
  1248. EXT4_XATTR_LEN(last->e_name_len);
  1249. if (total_size <= free && total_size < min_total_size) {
  1250. if (total_size < new_extra_isize) {
  1251. small_entry = last;
  1252. } else {
  1253. entry = last;
  1254. min_total_size = total_size;
  1255. }
  1256. }
  1257. }
  1258. if (entry == NULL) {
  1259. if (small_entry) {
  1260. entry = small_entry;
  1261. } else {
  1262. if (!tried_min_extra_isize &&
  1263. s_min_extra_isize) {
  1264. tried_min_extra_isize++;
  1265. new_extra_isize = s_min_extra_isize;
  1266. kfree(is); is = NULL;
  1267. kfree(bs); bs = NULL;
  1268. brelse(bh);
  1269. goto retry;
  1270. }
  1271. error = -1;
  1272. goto cleanup;
  1273. }
  1274. }
  1275. offs = le16_to_cpu(entry->e_value_offs);
  1276. size = le32_to_cpu(entry->e_value_size);
  1277. entry_size = EXT4_XATTR_LEN(entry->e_name_len);
  1278. i.name_index = entry->e_name_index,
  1279. buffer = kmalloc(EXT4_XATTR_SIZE(size), GFP_NOFS);
  1280. b_entry_name = kmalloc(entry->e_name_len + 1, GFP_NOFS);
  1281. if (!buffer || !b_entry_name) {
  1282. error = -ENOMEM;
  1283. goto cleanup;
  1284. }
  1285. /* Save the entry name and the entry value */
  1286. memcpy(buffer, (void *)IFIRST(header) + offs,
  1287. EXT4_XATTR_SIZE(size));
  1288. memcpy(b_entry_name, entry->e_name, entry->e_name_len);
  1289. b_entry_name[entry->e_name_len] = '\0';
  1290. i.name = b_entry_name;
  1291. error = ext4_get_inode_loc(inode, &is->iloc);
  1292. if (error)
  1293. goto cleanup;
  1294. error = ext4_xattr_ibody_find(inode, &i, is);
  1295. if (error)
  1296. goto cleanup;
  1297. /* Remove the chosen entry from the inode */
  1298. error = ext4_xattr_ibody_set(handle, inode, &i, is);
  1299. if (error)
  1300. goto cleanup;
  1301. entry = IFIRST(header);
  1302. if (entry_size + EXT4_XATTR_SIZE(size) >= new_extra_isize)
  1303. shift_bytes = new_extra_isize;
  1304. else
  1305. shift_bytes = entry_size + size;
  1306. /* Adjust the offsets and shift the remaining entries ahead */
  1307. ext4_xattr_shift_entries(entry, EXT4_I(inode)->i_extra_isize -
  1308. shift_bytes, (void *)raw_inode +
  1309. EXT4_GOOD_OLD_INODE_SIZE + extra_isize + shift_bytes,
  1310. (void *)header, total_ino - entry_size,
  1311. inode->i_sb->s_blocksize);
  1312. extra_isize += shift_bytes;
  1313. new_extra_isize -= shift_bytes;
  1314. EXT4_I(inode)->i_extra_isize = extra_isize;
  1315. i.name = b_entry_name;
  1316. i.value = buffer;
  1317. i.value_len = size;
  1318. error = ext4_xattr_block_find(inode, &i, bs);
  1319. if (error)
  1320. goto cleanup;
  1321. /* Add entry which was removed from the inode into the block */
  1322. error = ext4_xattr_block_set(handle, inode, &i, bs);
  1323. if (error)
  1324. goto cleanup;
  1325. kfree(b_entry_name);
  1326. kfree(buffer);
  1327. b_entry_name = NULL;
  1328. buffer = NULL;
  1329. brelse(is->iloc.bh);
  1330. kfree(is);
  1331. kfree(bs);
  1332. }
  1333. brelse(bh);
  1334. up_write(&EXT4_I(inode)->xattr_sem);
  1335. return 0;
  1336. cleanup:
  1337. kfree(b_entry_name);
  1338. kfree(buffer);
  1339. if (is)
  1340. brelse(is->iloc.bh);
  1341. kfree(is);
  1342. kfree(bs);
  1343. brelse(bh);
  1344. up_write(&EXT4_I(inode)->xattr_sem);
  1345. return error;
  1346. }
  1347. /*
  1348. * ext4_xattr_delete_inode()
  1349. *
  1350. * Free extended attribute resources associated with this inode. This
  1351. * is called immediately before an inode is freed. We have exclusive
  1352. * access to the inode.
  1353. */
  1354. void
  1355. ext4_xattr_delete_inode(handle_t *handle, struct inode *inode)
  1356. {
  1357. struct buffer_head *bh = NULL;
  1358. if (!EXT4_I(inode)->i_file_acl)
  1359. goto cleanup;
  1360. bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
  1361. if (!bh) {
  1362. EXT4_ERROR_INODE(inode, "block %llu read error",
  1363. EXT4_I(inode)->i_file_acl);
  1364. goto cleanup;
  1365. }
  1366. if (BHDR(bh)->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC) ||
  1367. BHDR(bh)->h_blocks != cpu_to_le32(1)) {
  1368. EXT4_ERROR_INODE(inode, "bad block %llu",
  1369. EXT4_I(inode)->i_file_acl);
  1370. goto cleanup;
  1371. }
  1372. ext4_xattr_release_block(handle, inode, bh);
  1373. EXT4_I(inode)->i_file_acl = 0;
  1374. cleanup:
  1375. brelse(bh);
  1376. }
  1377. /*
  1378. * ext4_xattr_put_super()
  1379. *
  1380. * This is called when a file system is unmounted.
  1381. */
  1382. void
  1383. ext4_xattr_put_super(struct super_block *sb)
  1384. {
  1385. mb_cache_shrink(sb->s_bdev);
  1386. }
  1387. /*
  1388. * ext4_xattr_cache_insert()
  1389. *
  1390. * Create a new entry in the extended attribute cache, and insert
  1391. * it unless such an entry is already in the cache.
  1392. *
  1393. * Returns 0, or a negative error number on failure.
  1394. */
  1395. static void
  1396. ext4_xattr_cache_insert(struct mb_cache *ext4_mb_cache, struct buffer_head *bh)
  1397. {
  1398. __u32 hash = le32_to_cpu(BHDR(bh)->h_hash);
  1399. struct mb_cache_entry *ce;
  1400. int error;
  1401. ce = mb_cache_entry_alloc(ext4_mb_cache, GFP_NOFS);
  1402. if (!ce) {
  1403. ea_bdebug(bh, "out of memory");
  1404. return;
  1405. }
  1406. error = mb_cache_entry_insert(ce, bh->b_bdev, bh->b_blocknr, hash);
  1407. if (error) {
  1408. mb_cache_entry_free(ce);
  1409. if (error == -EBUSY) {
  1410. ea_bdebug(bh, "already in cache");
  1411. error = 0;
  1412. }
  1413. } else {
  1414. ea_bdebug(bh, "inserting [%x]", (int)hash);
  1415. mb_cache_entry_release(ce);
  1416. }
  1417. }
  1418. /*
  1419. * ext4_xattr_cmp()
  1420. *
  1421. * Compare two extended attribute blocks for equality.
  1422. *
  1423. * Returns 0 if the blocks are equal, 1 if they differ, and
  1424. * a negative error number on errors.
  1425. */
  1426. static int
  1427. ext4_xattr_cmp(struct ext4_xattr_header *header1,
  1428. struct ext4_xattr_header *header2)
  1429. {
  1430. struct ext4_xattr_entry *entry1, *entry2;
  1431. entry1 = ENTRY(header1+1);
  1432. entry2 = ENTRY(header2+1);
  1433. while (!IS_LAST_ENTRY(entry1)) {
  1434. if (IS_LAST_ENTRY(entry2))
  1435. return 1;
  1436. if (entry1->e_hash != entry2->e_hash ||
  1437. entry1->e_name_index != entry2->e_name_index ||
  1438. entry1->e_name_len != entry2->e_name_len ||
  1439. entry1->e_value_size != entry2->e_value_size ||
  1440. memcmp(entry1->e_name, entry2->e_name, entry1->e_name_len))
  1441. return 1;
  1442. if (entry1->e_value_block != 0 || entry2->e_value_block != 0)
  1443. return -EFSCORRUPTED;
  1444. if (memcmp((char *)header1 + le16_to_cpu(entry1->e_value_offs),
  1445. (char *)header2 + le16_to_cpu(entry2->e_value_offs),
  1446. le32_to_cpu(entry1->e_value_size)))
  1447. return 1;
  1448. entry1 = EXT4_XATTR_NEXT(entry1);
  1449. entry2 = EXT4_XATTR_NEXT(entry2);
  1450. }
  1451. if (!IS_LAST_ENTRY(entry2))
  1452. return 1;
  1453. return 0;
  1454. }
  1455. /*
  1456. * ext4_xattr_cache_find()
  1457. *
  1458. * Find an identical extended attribute block.
  1459. *
  1460. * Returns a pointer to the block found, or NULL if such a block was
  1461. * not found or an error occurred.
  1462. */
  1463. static struct buffer_head *
  1464. ext4_xattr_cache_find(struct inode *inode, struct ext4_xattr_header *header,
  1465. struct mb_cache_entry **pce)
  1466. {
  1467. __u32 hash = le32_to_cpu(header->h_hash);
  1468. struct mb_cache_entry *ce;
  1469. struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
  1470. if (!header->h_hash)
  1471. return NULL; /* never share */
  1472. ea_idebug(inode, "looking for cached blocks [%x]", (int)hash);
  1473. again:
  1474. ce = mb_cache_entry_find_first(ext4_mb_cache, inode->i_sb->s_bdev,
  1475. hash);
  1476. while (ce) {
  1477. struct buffer_head *bh;
  1478. if (IS_ERR(ce)) {
  1479. if (PTR_ERR(ce) == -EAGAIN)
  1480. goto again;
  1481. break;
  1482. }
  1483. bh = sb_bread(inode->i_sb, ce->e_block);
  1484. if (!bh) {
  1485. EXT4_ERROR_INODE(inode, "block %lu read error",
  1486. (unsigned long) ce->e_block);
  1487. } else if (le32_to_cpu(BHDR(bh)->h_refcount) >=
  1488. EXT4_XATTR_REFCOUNT_MAX) {
  1489. ea_idebug(inode, "block %lu refcount %d>=%d",
  1490. (unsigned long) ce->e_block,
  1491. le32_to_cpu(BHDR(bh)->h_refcount),
  1492. EXT4_XATTR_REFCOUNT_MAX);
  1493. } else if (ext4_xattr_cmp(header, BHDR(bh)) == 0) {
  1494. *pce = ce;
  1495. return bh;
  1496. }
  1497. brelse(bh);
  1498. ce = mb_cache_entry_find_next(ce, inode->i_sb->s_bdev, hash);
  1499. }
  1500. return NULL;
  1501. }
  1502. #define NAME_HASH_SHIFT 5
  1503. #define VALUE_HASH_SHIFT 16
  1504. /*
  1505. * ext4_xattr_hash_entry()
  1506. *
  1507. * Compute the hash of an extended attribute.
  1508. */
  1509. static inline void ext4_xattr_hash_entry(struct ext4_xattr_header *header,
  1510. struct ext4_xattr_entry *entry)
  1511. {
  1512. __u32 hash = 0;
  1513. char *name = entry->e_name;
  1514. int n;
  1515. for (n = 0; n < entry->e_name_len; n++) {
  1516. hash = (hash << NAME_HASH_SHIFT) ^
  1517. (hash >> (8*sizeof(hash) - NAME_HASH_SHIFT)) ^
  1518. *name++;
  1519. }
  1520. if (entry->e_value_block == 0 && entry->e_value_size != 0) {
  1521. __le32 *value = (__le32 *)((char *)header +
  1522. le16_to_cpu(entry->e_value_offs));
  1523. for (n = (le32_to_cpu(entry->e_value_size) +
  1524. EXT4_XATTR_ROUND) >> EXT4_XATTR_PAD_BITS; n; n--) {
  1525. hash = (hash << VALUE_HASH_SHIFT) ^
  1526. (hash >> (8*sizeof(hash) - VALUE_HASH_SHIFT)) ^
  1527. le32_to_cpu(*value++);
  1528. }
  1529. }
  1530. entry->e_hash = cpu_to_le32(hash);
  1531. }
  1532. #undef NAME_HASH_SHIFT
  1533. #undef VALUE_HASH_SHIFT
  1534. #define BLOCK_HASH_SHIFT 16
  1535. /*
  1536. * ext4_xattr_rehash()
  1537. *
  1538. * Re-compute the extended attribute hash value after an entry has changed.
  1539. */
  1540. static void ext4_xattr_rehash(struct ext4_xattr_header *header,
  1541. struct ext4_xattr_entry *entry)
  1542. {
  1543. struct ext4_xattr_entry *here;
  1544. __u32 hash = 0;
  1545. ext4_xattr_hash_entry(header, entry);
  1546. here = ENTRY(header+1);
  1547. while (!IS_LAST_ENTRY(here)) {
  1548. if (!here->e_hash) {
  1549. /* Block is not shared if an entry's hash value == 0 */
  1550. hash = 0;
  1551. break;
  1552. }
  1553. hash = (hash << BLOCK_HASH_SHIFT) ^
  1554. (hash >> (8*sizeof(hash) - BLOCK_HASH_SHIFT)) ^
  1555. le32_to_cpu(here->e_hash);
  1556. here = EXT4_XATTR_NEXT(here);
  1557. }
  1558. header->h_hash = cpu_to_le32(hash);
  1559. }
  1560. #undef BLOCK_HASH_SHIFT
  1561. #define HASH_BUCKET_BITS 10
  1562. struct mb_cache *
  1563. ext4_xattr_create_cache(char *name)
  1564. {
  1565. return mb_cache_create(name, HASH_BUCKET_BITS);
  1566. }
  1567. void ext4_xattr_destroy_cache(struct mb_cache *cache)
  1568. {
  1569. if (cache)
  1570. mb_cache_destroy(cache);
  1571. }