xattr.c 45 KB

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