xattr.c 27 KB

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