xattr.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019
  1. /*
  2. * linux/fs/reiserfs/xattr.c
  3. *
  4. * Copyright (c) 2002 by Jeff Mahoney, <jeffm@suse.com>
  5. *
  6. */
  7. /*
  8. * In order to implement EA/ACLs in a clean, backwards compatible manner,
  9. * they are implemented as files in a "private" directory.
  10. * Each EA is in it's own file, with the directory layout like so (/ is assumed
  11. * to be relative to fs root). Inside the /.reiserfs_priv/xattrs directory,
  12. * directories named using the capital-hex form of the objectid and
  13. * generation number are used. Inside each directory are individual files
  14. * named with the name of the extended attribute.
  15. *
  16. * So, for objectid 12648430, we could have:
  17. * /.reiserfs_priv/xattrs/C0FFEE.0/system.posix_acl_access
  18. * /.reiserfs_priv/xattrs/C0FFEE.0/system.posix_acl_default
  19. * /.reiserfs_priv/xattrs/C0FFEE.0/user.Content-Type
  20. * .. or similar.
  21. *
  22. * The file contents are the text of the EA. The size is known based on the
  23. * stat data describing the file.
  24. *
  25. * In the case of system.posix_acl_access and system.posix_acl_default, since
  26. * these are special cases for filesystem ACLs, they are interpreted by the
  27. * kernel, in addition, they are negatively and positively cached and attached
  28. * to the inode so that unnecessary lookups are avoided.
  29. *
  30. * Locking works like so:
  31. * Directory components (xattr root, xattr dir) are protectd by their i_mutex.
  32. * The xattrs themselves are protected by the xattr_sem.
  33. */
  34. #include "reiserfs.h"
  35. #include <linux/capability.h>
  36. #include <linux/dcache.h>
  37. #include <linux/namei.h>
  38. #include <linux/errno.h>
  39. #include <linux/gfp.h>
  40. #include <linux/fs.h>
  41. #include <linux/file.h>
  42. #include <linux/pagemap.h>
  43. #include <linux/xattr.h>
  44. #include "xattr.h"
  45. #include "acl.h"
  46. #include <linux/uaccess.h>
  47. #include <net/checksum.h>
  48. #include <linux/stat.h>
  49. #include <linux/quotaops.h>
  50. #include <linux/security.h>
  51. #include <linux/posix_acl_xattr.h>
  52. #define PRIVROOT_NAME ".reiserfs_priv"
  53. #define XAROOT_NAME "xattrs"
  54. /*
  55. * Helpers for inode ops. We do this so that we don't have all the VFS
  56. * overhead and also for proper i_mutex annotation.
  57. * dir->i_mutex must be held for all of them.
  58. */
  59. #ifdef CONFIG_REISERFS_FS_XATTR
  60. static int xattr_create(struct inode *dir, struct dentry *dentry, int mode)
  61. {
  62. BUG_ON(!mutex_is_locked(&dir->i_mutex));
  63. return dir->i_op->create(dir, dentry, mode, true);
  64. }
  65. #endif
  66. static int xattr_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
  67. {
  68. BUG_ON(!mutex_is_locked(&dir->i_mutex));
  69. return dir->i_op->mkdir(dir, dentry, mode);
  70. }
  71. /*
  72. * We use I_MUTEX_CHILD here to silence lockdep. It's safe because xattr
  73. * mutation ops aren't called during rename or splace, which are the
  74. * only other users of I_MUTEX_CHILD. It violates the ordering, but that's
  75. * better than allocating another subclass just for this code.
  76. */
  77. static int xattr_unlink(struct inode *dir, struct dentry *dentry)
  78. {
  79. int error;
  80. BUG_ON(!mutex_is_locked(&dir->i_mutex));
  81. mutex_lock_nested(&d_inode(dentry)->i_mutex, I_MUTEX_CHILD);
  82. error = dir->i_op->unlink(dir, dentry);
  83. mutex_unlock(&d_inode(dentry)->i_mutex);
  84. if (!error)
  85. d_delete(dentry);
  86. return error;
  87. }
  88. static int xattr_rmdir(struct inode *dir, struct dentry *dentry)
  89. {
  90. int error;
  91. BUG_ON(!mutex_is_locked(&dir->i_mutex));
  92. mutex_lock_nested(&d_inode(dentry)->i_mutex, I_MUTEX_CHILD);
  93. error = dir->i_op->rmdir(dir, dentry);
  94. if (!error)
  95. d_inode(dentry)->i_flags |= S_DEAD;
  96. mutex_unlock(&d_inode(dentry)->i_mutex);
  97. if (!error)
  98. d_delete(dentry);
  99. return error;
  100. }
  101. #define xattr_may_create(flags) (!flags || flags & XATTR_CREATE)
  102. static struct dentry *open_xa_root(struct super_block *sb, int flags)
  103. {
  104. struct dentry *privroot = REISERFS_SB(sb)->priv_root;
  105. struct dentry *xaroot;
  106. if (d_really_is_negative(privroot))
  107. return ERR_PTR(-ENODATA);
  108. mutex_lock_nested(&d_inode(privroot)->i_mutex, I_MUTEX_XATTR);
  109. xaroot = dget(REISERFS_SB(sb)->xattr_root);
  110. if (!xaroot)
  111. xaroot = ERR_PTR(-ENODATA);
  112. else if (d_really_is_negative(xaroot)) {
  113. int err = -ENODATA;
  114. if (xattr_may_create(flags))
  115. err = xattr_mkdir(d_inode(privroot), xaroot, 0700);
  116. if (err) {
  117. dput(xaroot);
  118. xaroot = ERR_PTR(err);
  119. }
  120. }
  121. mutex_unlock(&d_inode(privroot)->i_mutex);
  122. return xaroot;
  123. }
  124. static struct dentry *open_xa_dir(const struct inode *inode, int flags)
  125. {
  126. struct dentry *xaroot, *xadir;
  127. char namebuf[17];
  128. xaroot = open_xa_root(inode->i_sb, flags);
  129. if (IS_ERR(xaroot))
  130. return xaroot;
  131. snprintf(namebuf, sizeof(namebuf), "%X.%X",
  132. le32_to_cpu(INODE_PKEY(inode)->k_objectid),
  133. inode->i_generation);
  134. mutex_lock_nested(&d_inode(xaroot)->i_mutex, I_MUTEX_XATTR);
  135. xadir = lookup_one_len(namebuf, xaroot, strlen(namebuf));
  136. if (!IS_ERR(xadir) && d_really_is_negative(xadir)) {
  137. int err = -ENODATA;
  138. if (xattr_may_create(flags))
  139. err = xattr_mkdir(d_inode(xaroot), xadir, 0700);
  140. if (err) {
  141. dput(xadir);
  142. xadir = ERR_PTR(err);
  143. }
  144. }
  145. mutex_unlock(&d_inode(xaroot)->i_mutex);
  146. dput(xaroot);
  147. return xadir;
  148. }
  149. /*
  150. * The following are side effects of other operations that aren't explicitly
  151. * modifying extended attributes. This includes operations such as permissions
  152. * or ownership changes, object deletions, etc.
  153. */
  154. struct reiserfs_dentry_buf {
  155. struct dir_context ctx;
  156. struct dentry *xadir;
  157. int count;
  158. int err;
  159. struct dentry *dentries[8];
  160. };
  161. static int
  162. fill_with_dentries(struct dir_context *ctx, const char *name, int namelen,
  163. loff_t offset, u64 ino, unsigned int d_type)
  164. {
  165. struct reiserfs_dentry_buf *dbuf =
  166. container_of(ctx, struct reiserfs_dentry_buf, ctx);
  167. struct dentry *dentry;
  168. WARN_ON_ONCE(!mutex_is_locked(&d_inode(dbuf->xadir)->i_mutex));
  169. if (dbuf->count == ARRAY_SIZE(dbuf->dentries))
  170. return -ENOSPC;
  171. if (name[0] == '.' && (namelen < 2 ||
  172. (namelen == 2 && name[1] == '.')))
  173. return 0;
  174. dentry = lookup_one_len(name, dbuf->xadir, namelen);
  175. if (IS_ERR(dentry)) {
  176. dbuf->err = PTR_ERR(dentry);
  177. return PTR_ERR(dentry);
  178. } else if (d_really_is_negative(dentry)) {
  179. /* A directory entry exists, but no file? */
  180. reiserfs_error(dentry->d_sb, "xattr-20003",
  181. "Corrupted directory: xattr %pd listed but "
  182. "not found for file %pd.\n",
  183. dentry, dbuf->xadir);
  184. dput(dentry);
  185. dbuf->err = -EIO;
  186. return -EIO;
  187. }
  188. dbuf->dentries[dbuf->count++] = dentry;
  189. return 0;
  190. }
  191. static void
  192. cleanup_dentry_buf(struct reiserfs_dentry_buf *buf)
  193. {
  194. int i;
  195. for (i = 0; i < buf->count; i++)
  196. if (buf->dentries[i])
  197. dput(buf->dentries[i]);
  198. }
  199. static int reiserfs_for_each_xattr(struct inode *inode,
  200. int (*action)(struct dentry *, void *),
  201. void *data)
  202. {
  203. struct dentry *dir;
  204. int i, err = 0;
  205. struct reiserfs_dentry_buf buf = {
  206. .ctx.actor = fill_with_dentries,
  207. };
  208. /* Skip out, an xattr has no xattrs associated with it */
  209. if (IS_PRIVATE(inode) || get_inode_sd_version(inode) == STAT_DATA_V1)
  210. return 0;
  211. dir = open_xa_dir(inode, XATTR_REPLACE);
  212. if (IS_ERR(dir)) {
  213. err = PTR_ERR(dir);
  214. goto out;
  215. } else if (d_really_is_negative(dir)) {
  216. err = 0;
  217. goto out_dir;
  218. }
  219. mutex_lock_nested(&d_inode(dir)->i_mutex, I_MUTEX_XATTR);
  220. buf.xadir = dir;
  221. while (1) {
  222. err = reiserfs_readdir_inode(d_inode(dir), &buf.ctx);
  223. if (err)
  224. break;
  225. if (buf.err) {
  226. err = buf.err;
  227. break;
  228. }
  229. if (!buf.count)
  230. break;
  231. for (i = 0; !err && i < buf.count && buf.dentries[i]; i++) {
  232. struct dentry *dentry = buf.dentries[i];
  233. if (!d_is_dir(dentry))
  234. err = action(dentry, data);
  235. dput(dentry);
  236. buf.dentries[i] = NULL;
  237. }
  238. if (err)
  239. break;
  240. buf.count = 0;
  241. }
  242. mutex_unlock(&d_inode(dir)->i_mutex);
  243. cleanup_dentry_buf(&buf);
  244. if (!err) {
  245. /*
  246. * We start a transaction here to avoid a ABBA situation
  247. * between the xattr root's i_mutex and the journal lock.
  248. * This doesn't incur much additional overhead since the
  249. * new transaction will just nest inside the
  250. * outer transaction.
  251. */
  252. int blocks = JOURNAL_PER_BALANCE_CNT * 2 + 2 +
  253. 4 * REISERFS_QUOTA_TRANS_BLOCKS(inode->i_sb);
  254. struct reiserfs_transaction_handle th;
  255. reiserfs_write_lock(inode->i_sb);
  256. err = journal_begin(&th, inode->i_sb, blocks);
  257. reiserfs_write_unlock(inode->i_sb);
  258. if (!err) {
  259. int jerror;
  260. mutex_lock_nested(&d_inode(dir->d_parent)->i_mutex,
  261. I_MUTEX_XATTR);
  262. err = action(dir, data);
  263. reiserfs_write_lock(inode->i_sb);
  264. jerror = journal_end(&th);
  265. reiserfs_write_unlock(inode->i_sb);
  266. mutex_unlock(&d_inode(dir->d_parent)->i_mutex);
  267. err = jerror ?: err;
  268. }
  269. }
  270. out_dir:
  271. dput(dir);
  272. out:
  273. /* -ENODATA isn't an error */
  274. if (err == -ENODATA)
  275. err = 0;
  276. return err;
  277. }
  278. static int delete_one_xattr(struct dentry *dentry, void *data)
  279. {
  280. struct inode *dir = d_inode(dentry->d_parent);
  281. /* This is the xattr dir, handle specially. */
  282. if (d_is_dir(dentry))
  283. return xattr_rmdir(dir, dentry);
  284. return xattr_unlink(dir, dentry);
  285. }
  286. static int chown_one_xattr(struct dentry *dentry, void *data)
  287. {
  288. struct iattr *attrs = data;
  289. int ia_valid = attrs->ia_valid;
  290. int err;
  291. /*
  292. * We only want the ownership bits. Otherwise, we'll do
  293. * things like change a directory to a regular file if
  294. * ATTR_MODE is set.
  295. */
  296. attrs->ia_valid &= (ATTR_UID|ATTR_GID);
  297. err = reiserfs_setattr(dentry, attrs);
  298. attrs->ia_valid = ia_valid;
  299. return err;
  300. }
  301. /* No i_mutex, but the inode is unconnected. */
  302. int reiserfs_delete_xattrs(struct inode *inode)
  303. {
  304. int err = reiserfs_for_each_xattr(inode, delete_one_xattr, NULL);
  305. if (err)
  306. reiserfs_warning(inode->i_sb, "jdm-20004",
  307. "Couldn't delete all xattrs (%d)\n", err);
  308. return err;
  309. }
  310. /* inode->i_mutex: down */
  311. int reiserfs_chown_xattrs(struct inode *inode, struct iattr *attrs)
  312. {
  313. int err = reiserfs_for_each_xattr(inode, chown_one_xattr, attrs);
  314. if (err)
  315. reiserfs_warning(inode->i_sb, "jdm-20007",
  316. "Couldn't chown all xattrs (%d)\n", err);
  317. return err;
  318. }
  319. #ifdef CONFIG_REISERFS_FS_XATTR
  320. /*
  321. * Returns a dentry corresponding to a specific extended attribute file
  322. * for the inode. If flags allow, the file is created. Otherwise, a
  323. * valid or negative dentry, or an error is returned.
  324. */
  325. static struct dentry *xattr_lookup(struct inode *inode, const char *name,
  326. int flags)
  327. {
  328. struct dentry *xadir, *xafile;
  329. int err = 0;
  330. xadir = open_xa_dir(inode, flags);
  331. if (IS_ERR(xadir))
  332. return ERR_CAST(xadir);
  333. mutex_lock_nested(&d_inode(xadir)->i_mutex, I_MUTEX_XATTR);
  334. xafile = lookup_one_len(name, xadir, strlen(name));
  335. if (IS_ERR(xafile)) {
  336. err = PTR_ERR(xafile);
  337. goto out;
  338. }
  339. if (d_really_is_positive(xafile) && (flags & XATTR_CREATE))
  340. err = -EEXIST;
  341. if (d_really_is_negative(xafile)) {
  342. err = -ENODATA;
  343. if (xattr_may_create(flags))
  344. err = xattr_create(d_inode(xadir), xafile,
  345. 0700|S_IFREG);
  346. }
  347. if (err)
  348. dput(xafile);
  349. out:
  350. mutex_unlock(&d_inode(xadir)->i_mutex);
  351. dput(xadir);
  352. if (err)
  353. return ERR_PTR(err);
  354. return xafile;
  355. }
  356. /* Internal operations on file data */
  357. static inline void reiserfs_put_page(struct page *page)
  358. {
  359. kunmap(page);
  360. page_cache_release(page);
  361. }
  362. static struct page *reiserfs_get_page(struct inode *dir, size_t n)
  363. {
  364. struct address_space *mapping = dir->i_mapping;
  365. struct page *page;
  366. /*
  367. * We can deadlock if we try to free dentries,
  368. * and an unlink/rmdir has just occurred - GFP_NOFS avoids this
  369. */
  370. mapping_set_gfp_mask(mapping, GFP_NOFS);
  371. page = read_mapping_page(mapping, n >> PAGE_CACHE_SHIFT, NULL);
  372. if (!IS_ERR(page)) {
  373. kmap(page);
  374. if (PageError(page))
  375. goto fail;
  376. }
  377. return page;
  378. fail:
  379. reiserfs_put_page(page);
  380. return ERR_PTR(-EIO);
  381. }
  382. static inline __u32 xattr_hash(const char *msg, int len)
  383. {
  384. return csum_partial(msg, len, 0);
  385. }
  386. int reiserfs_commit_write(struct file *f, struct page *page,
  387. unsigned from, unsigned to);
  388. static void update_ctime(struct inode *inode)
  389. {
  390. struct timespec now = current_fs_time(inode->i_sb);
  391. if (inode_unhashed(inode) || !inode->i_nlink ||
  392. timespec_equal(&inode->i_ctime, &now))
  393. return;
  394. inode->i_ctime = CURRENT_TIME_SEC;
  395. mark_inode_dirty(inode);
  396. }
  397. static int lookup_and_delete_xattr(struct inode *inode, const char *name)
  398. {
  399. int err = 0;
  400. struct dentry *dentry, *xadir;
  401. xadir = open_xa_dir(inode, XATTR_REPLACE);
  402. if (IS_ERR(xadir))
  403. return PTR_ERR(xadir);
  404. mutex_lock_nested(&d_inode(xadir)->i_mutex, I_MUTEX_XATTR);
  405. dentry = lookup_one_len(name, xadir, strlen(name));
  406. if (IS_ERR(dentry)) {
  407. err = PTR_ERR(dentry);
  408. goto out_dput;
  409. }
  410. if (d_really_is_positive(dentry)) {
  411. err = xattr_unlink(d_inode(xadir), dentry);
  412. update_ctime(inode);
  413. }
  414. dput(dentry);
  415. out_dput:
  416. mutex_unlock(&d_inode(xadir)->i_mutex);
  417. dput(xadir);
  418. return err;
  419. }
  420. /* Generic extended attribute operations that can be used by xa plugins */
  421. /*
  422. * inode->i_mutex: down
  423. */
  424. int
  425. reiserfs_xattr_set_handle(struct reiserfs_transaction_handle *th,
  426. struct inode *inode, const char *name,
  427. const void *buffer, size_t buffer_size, int flags)
  428. {
  429. int err = 0;
  430. struct dentry *dentry;
  431. struct page *page;
  432. char *data;
  433. size_t file_pos = 0;
  434. size_t buffer_pos = 0;
  435. size_t new_size;
  436. __u32 xahash = 0;
  437. if (get_inode_sd_version(inode) == STAT_DATA_V1)
  438. return -EOPNOTSUPP;
  439. if (!buffer) {
  440. err = lookup_and_delete_xattr(inode, name);
  441. return err;
  442. }
  443. dentry = xattr_lookup(inode, name, flags);
  444. if (IS_ERR(dentry))
  445. return PTR_ERR(dentry);
  446. down_write(&REISERFS_I(inode)->i_xattr_sem);
  447. xahash = xattr_hash(buffer, buffer_size);
  448. while (buffer_pos < buffer_size || buffer_pos == 0) {
  449. size_t chunk;
  450. size_t skip = 0;
  451. size_t page_offset = (file_pos & (PAGE_CACHE_SIZE - 1));
  452. if (buffer_size - buffer_pos > PAGE_CACHE_SIZE)
  453. chunk = PAGE_CACHE_SIZE;
  454. else
  455. chunk = buffer_size - buffer_pos;
  456. page = reiserfs_get_page(d_inode(dentry), file_pos);
  457. if (IS_ERR(page)) {
  458. err = PTR_ERR(page);
  459. goto out_unlock;
  460. }
  461. lock_page(page);
  462. data = page_address(page);
  463. if (file_pos == 0) {
  464. struct reiserfs_xattr_header *rxh;
  465. skip = file_pos = sizeof(struct reiserfs_xattr_header);
  466. if (chunk + skip > PAGE_CACHE_SIZE)
  467. chunk = PAGE_CACHE_SIZE - skip;
  468. rxh = (struct reiserfs_xattr_header *)data;
  469. rxh->h_magic = cpu_to_le32(REISERFS_XATTR_MAGIC);
  470. rxh->h_hash = cpu_to_le32(xahash);
  471. }
  472. reiserfs_write_lock(inode->i_sb);
  473. err = __reiserfs_write_begin(page, page_offset, chunk + skip);
  474. if (!err) {
  475. if (buffer)
  476. memcpy(data + skip, buffer + buffer_pos, chunk);
  477. err = reiserfs_commit_write(NULL, page, page_offset,
  478. page_offset + chunk +
  479. skip);
  480. }
  481. reiserfs_write_unlock(inode->i_sb);
  482. unlock_page(page);
  483. reiserfs_put_page(page);
  484. buffer_pos += chunk;
  485. file_pos += chunk;
  486. skip = 0;
  487. if (err || buffer_size == 0 || !buffer)
  488. break;
  489. }
  490. new_size = buffer_size + sizeof(struct reiserfs_xattr_header);
  491. if (!err && new_size < i_size_read(d_inode(dentry))) {
  492. struct iattr newattrs = {
  493. .ia_ctime = current_fs_time(inode->i_sb),
  494. .ia_size = new_size,
  495. .ia_valid = ATTR_SIZE | ATTR_CTIME,
  496. };
  497. mutex_lock_nested(&d_inode(dentry)->i_mutex, I_MUTEX_XATTR);
  498. inode_dio_wait(d_inode(dentry));
  499. err = reiserfs_setattr(dentry, &newattrs);
  500. mutex_unlock(&d_inode(dentry)->i_mutex);
  501. } else
  502. update_ctime(inode);
  503. out_unlock:
  504. up_write(&REISERFS_I(inode)->i_xattr_sem);
  505. dput(dentry);
  506. return err;
  507. }
  508. /* We need to start a transaction to maintain lock ordering */
  509. int reiserfs_xattr_set(struct inode *inode, const char *name,
  510. const void *buffer, size_t buffer_size, int flags)
  511. {
  512. struct reiserfs_transaction_handle th;
  513. int error, error2;
  514. size_t jbegin_count = reiserfs_xattr_nblocks(inode, buffer_size);
  515. if (!(flags & XATTR_REPLACE))
  516. jbegin_count += reiserfs_xattr_jcreate_nblocks(inode);
  517. reiserfs_write_lock(inode->i_sb);
  518. error = journal_begin(&th, inode->i_sb, jbegin_count);
  519. reiserfs_write_unlock(inode->i_sb);
  520. if (error) {
  521. return error;
  522. }
  523. error = reiserfs_xattr_set_handle(&th, inode, name,
  524. buffer, buffer_size, flags);
  525. reiserfs_write_lock(inode->i_sb);
  526. error2 = journal_end(&th);
  527. reiserfs_write_unlock(inode->i_sb);
  528. if (error == 0)
  529. error = error2;
  530. return error;
  531. }
  532. /*
  533. * inode->i_mutex: down
  534. */
  535. int
  536. reiserfs_xattr_get(struct inode *inode, const char *name, void *buffer,
  537. size_t buffer_size)
  538. {
  539. ssize_t err = 0;
  540. struct dentry *dentry;
  541. size_t isize;
  542. size_t file_pos = 0;
  543. size_t buffer_pos = 0;
  544. struct page *page;
  545. __u32 hash = 0;
  546. if (name == NULL)
  547. return -EINVAL;
  548. /*
  549. * We can't have xattrs attached to v1 items since they don't have
  550. * generation numbers
  551. */
  552. if (get_inode_sd_version(inode) == STAT_DATA_V1)
  553. return -EOPNOTSUPP;
  554. dentry = xattr_lookup(inode, name, XATTR_REPLACE);
  555. if (IS_ERR(dentry)) {
  556. err = PTR_ERR(dentry);
  557. goto out;
  558. }
  559. down_read(&REISERFS_I(inode)->i_xattr_sem);
  560. isize = i_size_read(d_inode(dentry));
  561. /* Just return the size needed */
  562. if (buffer == NULL) {
  563. err = isize - sizeof(struct reiserfs_xattr_header);
  564. goto out_unlock;
  565. }
  566. if (buffer_size < isize - sizeof(struct reiserfs_xattr_header)) {
  567. err = -ERANGE;
  568. goto out_unlock;
  569. }
  570. while (file_pos < isize) {
  571. size_t chunk;
  572. char *data;
  573. size_t skip = 0;
  574. if (isize - file_pos > PAGE_CACHE_SIZE)
  575. chunk = PAGE_CACHE_SIZE;
  576. else
  577. chunk = isize - file_pos;
  578. page = reiserfs_get_page(d_inode(dentry), file_pos);
  579. if (IS_ERR(page)) {
  580. err = PTR_ERR(page);
  581. goto out_unlock;
  582. }
  583. lock_page(page);
  584. data = page_address(page);
  585. if (file_pos == 0) {
  586. struct reiserfs_xattr_header *rxh =
  587. (struct reiserfs_xattr_header *)data;
  588. skip = file_pos = sizeof(struct reiserfs_xattr_header);
  589. chunk -= skip;
  590. /* Magic doesn't match up.. */
  591. if (rxh->h_magic != cpu_to_le32(REISERFS_XATTR_MAGIC)) {
  592. unlock_page(page);
  593. reiserfs_put_page(page);
  594. reiserfs_warning(inode->i_sb, "jdm-20001",
  595. "Invalid magic for xattr (%s) "
  596. "associated with %k", name,
  597. INODE_PKEY(inode));
  598. err = -EIO;
  599. goto out_unlock;
  600. }
  601. hash = le32_to_cpu(rxh->h_hash);
  602. }
  603. memcpy(buffer + buffer_pos, data + skip, chunk);
  604. unlock_page(page);
  605. reiserfs_put_page(page);
  606. file_pos += chunk;
  607. buffer_pos += chunk;
  608. skip = 0;
  609. }
  610. err = isize - sizeof(struct reiserfs_xattr_header);
  611. if (xattr_hash(buffer, isize - sizeof(struct reiserfs_xattr_header)) !=
  612. hash) {
  613. reiserfs_warning(inode->i_sb, "jdm-20002",
  614. "Invalid hash for xattr (%s) associated "
  615. "with %k", name, INODE_PKEY(inode));
  616. err = -EIO;
  617. }
  618. out_unlock:
  619. up_read(&REISERFS_I(inode)->i_xattr_sem);
  620. dput(dentry);
  621. out:
  622. return err;
  623. }
  624. /*
  625. * In order to implement different sets of xattr operations for each xattr
  626. * prefix with the generic xattr API, a filesystem should create a
  627. * null-terminated array of struct xattr_handler (one for each prefix) and
  628. * hang a pointer to it off of the s_xattr field of the superblock.
  629. *
  630. * The generic_fooxattr() functions will use this list to dispatch xattr
  631. * operations to the correct xattr_handler.
  632. */
  633. #define for_each_xattr_handler(handlers, handler) \
  634. for ((handler) = *(handlers)++; \
  635. (handler) != NULL; \
  636. (handler) = *(handlers)++)
  637. /* This is the implementation for the xattr plugin infrastructure */
  638. static inline const struct xattr_handler *
  639. find_xattr_handler_prefix(const struct xattr_handler **handlers,
  640. const char *name)
  641. {
  642. const struct xattr_handler *xah;
  643. if (!handlers)
  644. return NULL;
  645. for_each_xattr_handler(handlers, xah) {
  646. if (strncmp(xah->prefix, name, strlen(xah->prefix)) == 0)
  647. break;
  648. }
  649. return xah;
  650. }
  651. struct listxattr_buf {
  652. struct dir_context ctx;
  653. size_t size;
  654. size_t pos;
  655. char *buf;
  656. struct dentry *dentry;
  657. };
  658. static int listxattr_filler(struct dir_context *ctx, const char *name,
  659. int namelen, loff_t offset, u64 ino,
  660. unsigned int d_type)
  661. {
  662. struct listxattr_buf *b =
  663. container_of(ctx, struct listxattr_buf, ctx);
  664. size_t size;
  665. if (name[0] != '.' ||
  666. (namelen != 1 && (name[1] != '.' || namelen != 2))) {
  667. const struct xattr_handler *handler;
  668. handler = find_xattr_handler_prefix(b->dentry->d_sb->s_xattr,
  669. name);
  670. if (!handler) /* Unsupported xattr name */
  671. return 0;
  672. if (b->buf) {
  673. size = handler->list(handler, b->dentry,
  674. b->buf + b->pos, b->size, name,
  675. namelen);
  676. if (b->pos + size > b->size) {
  677. b->pos = -ERANGE;
  678. return -ERANGE;
  679. }
  680. } else {
  681. size = handler->list(handler, b->dentry,
  682. NULL, 0, name, namelen);
  683. }
  684. b->pos += size;
  685. }
  686. return 0;
  687. }
  688. /*
  689. * Inode operation listxattr()
  690. *
  691. * We totally ignore the generic listxattr here because it would be stupid
  692. * not to. Since the xattrs are organized in a directory, we can just
  693. * readdir to find them.
  694. */
  695. ssize_t reiserfs_listxattr(struct dentry * dentry, char *buffer, size_t size)
  696. {
  697. struct dentry *dir;
  698. int err = 0;
  699. struct listxattr_buf buf = {
  700. .ctx.actor = listxattr_filler,
  701. .dentry = dentry,
  702. .buf = buffer,
  703. .size = buffer ? size : 0,
  704. };
  705. if (d_really_is_negative(dentry))
  706. return -EINVAL;
  707. if (!dentry->d_sb->s_xattr ||
  708. get_inode_sd_version(d_inode(dentry)) == STAT_DATA_V1)
  709. return -EOPNOTSUPP;
  710. dir = open_xa_dir(d_inode(dentry), XATTR_REPLACE);
  711. if (IS_ERR(dir)) {
  712. err = PTR_ERR(dir);
  713. if (err == -ENODATA)
  714. err = 0; /* Not an error if there aren't any xattrs */
  715. goto out;
  716. }
  717. mutex_lock_nested(&d_inode(dir)->i_mutex, I_MUTEX_XATTR);
  718. err = reiserfs_readdir_inode(d_inode(dir), &buf.ctx);
  719. mutex_unlock(&d_inode(dir)->i_mutex);
  720. if (!err)
  721. err = buf.pos;
  722. dput(dir);
  723. out:
  724. return err;
  725. }
  726. static int create_privroot(struct dentry *dentry)
  727. {
  728. int err;
  729. struct inode *inode = d_inode(dentry->d_parent);
  730. WARN_ON_ONCE(!mutex_is_locked(&inode->i_mutex));
  731. err = xattr_mkdir(inode, dentry, 0700);
  732. if (err || d_really_is_negative(dentry)) {
  733. reiserfs_warning(dentry->d_sb, "jdm-20006",
  734. "xattrs/ACLs enabled and couldn't "
  735. "find/create .reiserfs_priv. "
  736. "Failing mount.");
  737. return -EOPNOTSUPP;
  738. }
  739. d_inode(dentry)->i_flags |= S_PRIVATE;
  740. reiserfs_info(dentry->d_sb, "Created %s - reserved for xattr "
  741. "storage.\n", PRIVROOT_NAME);
  742. return 0;
  743. }
  744. #else
  745. int __init reiserfs_xattr_register_handlers(void) { return 0; }
  746. void reiserfs_xattr_unregister_handlers(void) {}
  747. static int create_privroot(struct dentry *dentry) { return 0; }
  748. #endif
  749. /* Actual operations that are exported to VFS-land */
  750. static const struct xattr_handler *reiserfs_xattr_handlers[] = {
  751. #ifdef CONFIG_REISERFS_FS_XATTR
  752. &reiserfs_xattr_user_handler,
  753. &reiserfs_xattr_trusted_handler,
  754. #endif
  755. #ifdef CONFIG_REISERFS_FS_SECURITY
  756. &reiserfs_xattr_security_handler,
  757. #endif
  758. #ifdef CONFIG_REISERFS_FS_POSIX_ACL
  759. &posix_acl_access_xattr_handler,
  760. &posix_acl_default_xattr_handler,
  761. #endif
  762. NULL
  763. };
  764. static int xattr_mount_check(struct super_block *s)
  765. {
  766. /*
  767. * We need generation numbers to ensure that the oid mapping is correct
  768. * v3.5 filesystems don't have them.
  769. */
  770. if (old_format_only(s)) {
  771. if (reiserfs_xattrs_optional(s)) {
  772. /*
  773. * Old format filesystem, but optional xattrs have
  774. * been enabled. Error out.
  775. */
  776. reiserfs_warning(s, "jdm-2005",
  777. "xattrs/ACLs not supported "
  778. "on pre-v3.6 format filesystems. "
  779. "Failing mount.");
  780. return -EOPNOTSUPP;
  781. }
  782. }
  783. return 0;
  784. }
  785. int reiserfs_permission(struct inode *inode, int mask)
  786. {
  787. /*
  788. * We don't do permission checks on the internal objects.
  789. * Permissions are determined by the "owning" object.
  790. */
  791. if (IS_PRIVATE(inode))
  792. return 0;
  793. return generic_permission(inode, mask);
  794. }
  795. static int xattr_hide_revalidate(struct dentry *dentry, unsigned int flags)
  796. {
  797. return -EPERM;
  798. }
  799. static const struct dentry_operations xattr_lookup_poison_ops = {
  800. .d_revalidate = xattr_hide_revalidate,
  801. };
  802. int reiserfs_lookup_privroot(struct super_block *s)
  803. {
  804. struct dentry *dentry;
  805. int err = 0;
  806. /* If we don't have the privroot located yet - go find it */
  807. mutex_lock(&d_inode(s->s_root)->i_mutex);
  808. dentry = lookup_one_len(PRIVROOT_NAME, s->s_root,
  809. strlen(PRIVROOT_NAME));
  810. if (!IS_ERR(dentry)) {
  811. REISERFS_SB(s)->priv_root = dentry;
  812. d_set_d_op(dentry, &xattr_lookup_poison_ops);
  813. if (d_really_is_positive(dentry))
  814. d_inode(dentry)->i_flags |= S_PRIVATE;
  815. } else
  816. err = PTR_ERR(dentry);
  817. mutex_unlock(&d_inode(s->s_root)->i_mutex);
  818. return err;
  819. }
  820. /*
  821. * We need to take a copy of the mount flags since things like
  822. * MS_RDONLY don't get set until *after* we're called.
  823. * mount_flags != mount_options
  824. */
  825. int reiserfs_xattr_init(struct super_block *s, int mount_flags)
  826. {
  827. int err = 0;
  828. struct dentry *privroot = REISERFS_SB(s)->priv_root;
  829. err = xattr_mount_check(s);
  830. if (err)
  831. goto error;
  832. if (d_really_is_negative(privroot) && !(mount_flags & MS_RDONLY)) {
  833. mutex_lock(&d_inode(s->s_root)->i_mutex);
  834. err = create_privroot(REISERFS_SB(s)->priv_root);
  835. mutex_unlock(&d_inode(s->s_root)->i_mutex);
  836. }
  837. if (d_really_is_positive(privroot)) {
  838. s->s_xattr = reiserfs_xattr_handlers;
  839. mutex_lock(&d_inode(privroot)->i_mutex);
  840. if (!REISERFS_SB(s)->xattr_root) {
  841. struct dentry *dentry;
  842. dentry = lookup_one_len(XAROOT_NAME, privroot,
  843. strlen(XAROOT_NAME));
  844. if (!IS_ERR(dentry))
  845. REISERFS_SB(s)->xattr_root = dentry;
  846. else
  847. err = PTR_ERR(dentry);
  848. }
  849. mutex_unlock(&d_inode(privroot)->i_mutex);
  850. }
  851. error:
  852. if (err) {
  853. clear_bit(REISERFS_XATTRS_USER, &REISERFS_SB(s)->s_mount_opt);
  854. clear_bit(REISERFS_POSIXACL, &REISERFS_SB(s)->s_mount_opt);
  855. }
  856. /* The super_block MS_POSIXACL must mirror the (no)acl mount option. */
  857. if (reiserfs_posixacl(s))
  858. s->s_flags |= MS_POSIXACL;
  859. else
  860. s->s_flags &= ~MS_POSIXACL;
  861. return err;
  862. }