xattr.c 46 KB

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