xattr.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269
  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. #include <linux/reiserfs_fs.h>
  31. #include <linux/capability.h>
  32. #include <linux/dcache.h>
  33. #include <linux/namei.h>
  34. #include <linux/errno.h>
  35. #include <linux/fs.h>
  36. #include <linux/file.h>
  37. #include <linux/pagemap.h>
  38. #include <linux/xattr.h>
  39. #include <linux/reiserfs_xattr.h>
  40. #include <linux/reiserfs_acl.h>
  41. #include <asm/uaccess.h>
  42. #include <net/checksum.h>
  43. #include <linux/smp_lock.h>
  44. #include <linux/stat.h>
  45. #define FL_READONLY 128
  46. #define FL_DIR_SEM_HELD 256
  47. #define PRIVROOT_NAME ".reiserfs_priv"
  48. #define XAROOT_NAME "xattrs"
  49. static struct reiserfs_xattr_handler *find_xattr_handler_prefix(const char
  50. *prefix);
  51. /* Returns the dentry referring to the root of the extended attribute
  52. * directory tree. If it has already been retrieved, it is used. If it
  53. * hasn't been created and the flags indicate creation is allowed, we
  54. * attempt to create it. On error, we return a pointer-encoded error.
  55. */
  56. static struct dentry *get_xa_root(struct super_block *sb, int flags)
  57. {
  58. struct dentry *privroot = dget(REISERFS_SB(sb)->priv_root);
  59. struct dentry *xaroot;
  60. /* This needs to be created at mount-time */
  61. if (!privroot)
  62. return ERR_PTR(-ENODATA);
  63. mutex_lock_nested(&privroot->d_inode->i_mutex, I_MUTEX_XATTR);
  64. if (REISERFS_SB(sb)->xattr_root) {
  65. xaroot = dget(REISERFS_SB(sb)->xattr_root);
  66. goto out;
  67. }
  68. xaroot = lookup_one_len(XAROOT_NAME, privroot, strlen(XAROOT_NAME));
  69. if (IS_ERR(xaroot)) {
  70. goto out;
  71. } else if (!xaroot->d_inode) {
  72. int err = -ENODATA;
  73. if (flags == 0 || flags & XATTR_CREATE)
  74. err = privroot->d_inode->i_op->mkdir(privroot->d_inode,
  75. xaroot, 0700);
  76. if (err) {
  77. dput(xaroot);
  78. xaroot = ERR_PTR(err);
  79. goto out;
  80. }
  81. }
  82. REISERFS_SB(sb)->xattr_root = dget(xaroot);
  83. out:
  84. mutex_unlock(&privroot->d_inode->i_mutex);
  85. dput(privroot);
  86. return xaroot;
  87. }
  88. /* Opens the directory corresponding to the inode's extended attribute store.
  89. * If flags allow, the tree to the directory may be created. If creation is
  90. * prohibited, -ENODATA is returned. */
  91. static struct dentry *open_xa_dir(const struct inode *inode, int flags)
  92. {
  93. struct dentry *xaroot, *xadir;
  94. char namebuf[17];
  95. xaroot = get_xa_root(inode->i_sb, flags);
  96. if (IS_ERR(xaroot))
  97. return xaroot;
  98. /* ok, we have xaroot open */
  99. snprintf(namebuf, sizeof(namebuf), "%X.%X",
  100. le32_to_cpu(INODE_PKEY(inode)->k_objectid),
  101. inode->i_generation);
  102. xadir = lookup_one_len(namebuf, xaroot, strlen(namebuf));
  103. if (IS_ERR(xadir)) {
  104. dput(xaroot);
  105. return xadir;
  106. }
  107. if (!xadir->d_inode) {
  108. int err;
  109. if (flags == 0 || flags & XATTR_CREATE) {
  110. /* Although there is nothing else trying to create this directory,
  111. * another directory with the same hash may be created, so we need
  112. * to protect against that */
  113. err =
  114. xaroot->d_inode->i_op->mkdir(xaroot->d_inode, xadir,
  115. 0700);
  116. if (err) {
  117. dput(xaroot);
  118. dput(xadir);
  119. return ERR_PTR(err);
  120. }
  121. }
  122. if (!xadir->d_inode) {
  123. dput(xaroot);
  124. dput(xadir);
  125. return ERR_PTR(-ENODATA);
  126. }
  127. }
  128. dput(xaroot);
  129. return xadir;
  130. }
  131. /* Returns a dentry corresponding to a specific extended attribute file
  132. * for the inode. If flags allow, the file is created. Otherwise, a
  133. * valid or negative dentry, or an error is returned. */
  134. static struct dentry *get_xa_file_dentry(const struct inode *inode,
  135. const char *name, int flags)
  136. {
  137. struct dentry *xadir, *xafile;
  138. int err = 0;
  139. xadir = open_xa_dir(inode, flags);
  140. if (IS_ERR(xadir)) {
  141. return ERR_CAST(xadir);
  142. } else if (!xadir->d_inode) {
  143. dput(xadir);
  144. return ERR_PTR(-ENODATA);
  145. }
  146. xafile = lookup_one_len(name, xadir, strlen(name));
  147. if (IS_ERR(xafile)) {
  148. dput(xadir);
  149. return ERR_CAST(xafile);
  150. }
  151. if (xafile->d_inode) { /* file exists */
  152. if (flags & XATTR_CREATE) {
  153. err = -EEXIST;
  154. dput(xafile);
  155. goto out;
  156. }
  157. } else if (flags & XATTR_REPLACE || flags & FL_READONLY) {
  158. goto out;
  159. } else {
  160. /* inode->i_mutex is down, so nothing else can try to create
  161. * the same xattr */
  162. err = xadir->d_inode->i_op->create(xadir->d_inode, xafile,
  163. 0700 | S_IFREG, NULL);
  164. if (err) {
  165. dput(xafile);
  166. goto out;
  167. }
  168. }
  169. out:
  170. dput(xadir);
  171. if (err)
  172. xafile = ERR_PTR(err);
  173. else if (!xafile->d_inode) {
  174. dput(xafile);
  175. xafile = ERR_PTR(-ENODATA);
  176. }
  177. return xafile;
  178. }
  179. /*
  180. * this is very similar to fs/reiserfs/dir.c:reiserfs_readdir, but
  181. * we need to drop the path before calling the filldir struct. That
  182. * would be a big performance hit to the non-xattr case, so I've copied
  183. * the whole thing for now. --clm
  184. *
  185. * the big difference is that I go backwards through the directory,
  186. * and don't mess with f->f_pos, but the idea is the same. Do some
  187. * action on each and every entry in the directory.
  188. *
  189. * we're called with i_mutex held, so there are no worries about the directory
  190. * changing underneath us.
  191. */
  192. static int __xattr_readdir(struct inode *inode, void *dirent, filldir_t filldir)
  193. {
  194. struct cpu_key pos_key; /* key of current position in the directory (key of directory entry) */
  195. INITIALIZE_PATH(path_to_entry);
  196. struct buffer_head *bh;
  197. int entry_num;
  198. struct item_head *ih, tmp_ih;
  199. int search_res;
  200. char *local_buf;
  201. loff_t next_pos;
  202. char small_buf[32]; /* avoid kmalloc if we can */
  203. struct reiserfs_de_head *deh;
  204. int d_reclen;
  205. char *d_name;
  206. off_t d_off;
  207. ino_t d_ino;
  208. struct reiserfs_dir_entry de;
  209. /* form key for search the next directory entry using f_pos field of
  210. file structure */
  211. next_pos = max_reiserfs_offset(inode);
  212. while (1) {
  213. research:
  214. if (next_pos <= DOT_DOT_OFFSET)
  215. break;
  216. make_cpu_key(&pos_key, inode, next_pos, TYPE_DIRENTRY, 3);
  217. search_res =
  218. search_by_entry_key(inode->i_sb, &pos_key, &path_to_entry,
  219. &de);
  220. if (search_res == IO_ERROR) {
  221. // FIXME: we could just skip part of directory which could
  222. // not be read
  223. pathrelse(&path_to_entry);
  224. return -EIO;
  225. }
  226. if (search_res == NAME_NOT_FOUND)
  227. de.de_entry_num--;
  228. set_de_name_and_namelen(&de);
  229. entry_num = de.de_entry_num;
  230. deh = &(de.de_deh[entry_num]);
  231. bh = de.de_bh;
  232. ih = de.de_ih;
  233. if (!is_direntry_le_ih(ih)) {
  234. reiserfs_warning(inode->i_sb, "not direntry %h", ih);
  235. break;
  236. }
  237. copy_item_head(&tmp_ih, ih);
  238. /* we must have found item, that is item of this directory, */
  239. RFALSE(COMP_SHORT_KEYS(&(ih->ih_key), &pos_key),
  240. "vs-9000: found item %h does not match to dir we readdir %K",
  241. ih, &pos_key);
  242. if (deh_offset(deh) <= DOT_DOT_OFFSET) {
  243. break;
  244. }
  245. /* look for the previous entry in the directory */
  246. next_pos = deh_offset(deh) - 1;
  247. if (!de_visible(deh))
  248. /* it is hidden entry */
  249. continue;
  250. d_reclen = entry_length(bh, ih, entry_num);
  251. d_name = B_I_DEH_ENTRY_FILE_NAME(bh, ih, deh);
  252. d_off = deh_offset(deh);
  253. d_ino = deh_objectid(deh);
  254. if (!d_name[d_reclen - 1])
  255. d_reclen = strlen(d_name);
  256. if (d_reclen > REISERFS_MAX_NAME(inode->i_sb->s_blocksize)) {
  257. /* too big to send back to VFS */
  258. continue;
  259. }
  260. /* Ignore the .reiserfs_priv entry */
  261. if (reiserfs_xattrs(inode->i_sb) &&
  262. !old_format_only(inode->i_sb) &&
  263. deh_objectid(deh) ==
  264. le32_to_cpu(INODE_PKEY
  265. (REISERFS_SB(inode->i_sb)->priv_root->d_inode)->
  266. k_objectid))
  267. continue;
  268. if (d_reclen <= 32) {
  269. local_buf = small_buf;
  270. } else {
  271. local_buf = kmalloc(d_reclen, GFP_NOFS);
  272. if (!local_buf) {
  273. pathrelse(&path_to_entry);
  274. return -ENOMEM;
  275. }
  276. if (item_moved(&tmp_ih, &path_to_entry)) {
  277. kfree(local_buf);
  278. /* sigh, must retry. Do this same offset again */
  279. next_pos = d_off;
  280. goto research;
  281. }
  282. }
  283. // Note, that we copy name to user space via temporary
  284. // buffer (local_buf) because filldir will block if
  285. // user space buffer is swapped out. At that time
  286. // entry can move to somewhere else
  287. memcpy(local_buf, d_name, d_reclen);
  288. /* the filldir function might need to start transactions,
  289. * or do who knows what. Release the path now that we've
  290. * copied all the important stuff out of the deh
  291. */
  292. pathrelse(&path_to_entry);
  293. if (filldir(dirent, local_buf, d_reclen, d_off, d_ino,
  294. DT_UNKNOWN) < 0) {
  295. if (local_buf != small_buf) {
  296. kfree(local_buf);
  297. }
  298. goto end;
  299. }
  300. if (local_buf != small_buf) {
  301. kfree(local_buf);
  302. }
  303. } /* while */
  304. end:
  305. pathrelse(&path_to_entry);
  306. return 0;
  307. }
  308. /*
  309. * this could be done with dedicated readdir ops for the xattr files,
  310. * but I want to get something working asap
  311. * this is stolen from vfs_readdir
  312. *
  313. */
  314. static
  315. int xattr_readdir(struct inode *inode, filldir_t filler, void *buf)
  316. {
  317. int res = -ENOENT;
  318. mutex_lock_nested(&inode->i_mutex, I_MUTEX_XATTR);
  319. if (!IS_DEADDIR(inode)) {
  320. lock_kernel();
  321. res = __xattr_readdir(inode, buf, filler);
  322. unlock_kernel();
  323. }
  324. mutex_unlock(&inode->i_mutex);
  325. return res;
  326. }
  327. /* Internal operations on file data */
  328. static inline void reiserfs_put_page(struct page *page)
  329. {
  330. kunmap(page);
  331. page_cache_release(page);
  332. }
  333. static struct page *reiserfs_get_page(struct inode *dir, unsigned long n)
  334. {
  335. struct address_space *mapping = dir->i_mapping;
  336. struct page *page;
  337. /* We can deadlock if we try to free dentries,
  338. and an unlink/rmdir has just occured - GFP_NOFS avoids this */
  339. mapping_set_gfp_mask(mapping, GFP_NOFS);
  340. page = read_mapping_page(mapping, n, NULL);
  341. if (!IS_ERR(page)) {
  342. kmap(page);
  343. if (PageError(page))
  344. goto fail;
  345. }
  346. return page;
  347. fail:
  348. reiserfs_put_page(page);
  349. return ERR_PTR(-EIO);
  350. }
  351. static inline __u32 xattr_hash(const char *msg, int len)
  352. {
  353. return csum_partial(msg, len, 0);
  354. }
  355. int reiserfs_commit_write(struct file *f, struct page *page,
  356. unsigned from, unsigned to);
  357. int reiserfs_prepare_write(struct file *f, struct page *page,
  358. unsigned from, unsigned to);
  359. /* Generic extended attribute operations that can be used by xa plugins */
  360. /*
  361. * inode->i_mutex: down
  362. */
  363. int
  364. reiserfs_xattr_set(struct inode *inode, const char *name, const void *buffer,
  365. size_t buffer_size, int flags)
  366. {
  367. int err = 0;
  368. struct dentry *dentry;
  369. struct page *page;
  370. char *data;
  371. struct address_space *mapping;
  372. size_t file_pos = 0;
  373. size_t buffer_pos = 0;
  374. struct inode *xinode;
  375. struct iattr newattrs;
  376. __u32 xahash = 0;
  377. if (get_inode_sd_version(inode) == STAT_DATA_V1)
  378. return -EOPNOTSUPP;
  379. /* Empty xattrs are ok, they're just empty files, no hash */
  380. if (buffer && buffer_size)
  381. xahash = xattr_hash(buffer, buffer_size);
  382. open_file:
  383. dentry = get_xa_file_dentry(inode, name, flags);
  384. if (IS_ERR(dentry)) {
  385. err = PTR_ERR(dentry);
  386. goto out;
  387. }
  388. xinode = dentry->d_inode;
  389. REISERFS_I(inode)->i_flags |= i_has_xattr_dir;
  390. /* we need to copy it off.. */
  391. if (xinode->i_nlink > 1) {
  392. dput(dentry);
  393. err = reiserfs_xattr_del(inode, name);
  394. if (err < 0)
  395. goto out;
  396. /* We just killed the old one, we're not replacing anymore */
  397. if (flags & XATTR_REPLACE)
  398. flags &= ~XATTR_REPLACE;
  399. goto open_file;
  400. }
  401. /* Resize it so we're ok to write there */
  402. newattrs.ia_size = buffer_size;
  403. newattrs.ia_valid = ATTR_SIZE | ATTR_CTIME;
  404. mutex_lock_nested(&xinode->i_mutex, I_MUTEX_XATTR);
  405. err = notify_change(dentry, &newattrs);
  406. if (err)
  407. goto out_filp;
  408. mapping = xinode->i_mapping;
  409. while (buffer_pos < buffer_size || buffer_pos == 0) {
  410. size_t chunk;
  411. size_t skip = 0;
  412. size_t page_offset = (file_pos & (PAGE_CACHE_SIZE - 1));
  413. if (buffer_size - buffer_pos > PAGE_CACHE_SIZE)
  414. chunk = PAGE_CACHE_SIZE;
  415. else
  416. chunk = buffer_size - buffer_pos;
  417. page = reiserfs_get_page(xinode, file_pos >> PAGE_CACHE_SHIFT);
  418. if (IS_ERR(page)) {
  419. err = PTR_ERR(page);
  420. goto out_filp;
  421. }
  422. lock_page(page);
  423. data = page_address(page);
  424. if (file_pos == 0) {
  425. struct reiserfs_xattr_header *rxh;
  426. skip = file_pos = sizeof(struct reiserfs_xattr_header);
  427. if (chunk + skip > PAGE_CACHE_SIZE)
  428. chunk = PAGE_CACHE_SIZE - skip;
  429. rxh = (struct reiserfs_xattr_header *)data;
  430. rxh->h_magic = cpu_to_le32(REISERFS_XATTR_MAGIC);
  431. rxh->h_hash = cpu_to_le32(xahash);
  432. }
  433. err = reiserfs_prepare_write(NULL, page, page_offset,
  434. page_offset + chunk + skip);
  435. if (!err) {
  436. if (buffer)
  437. memcpy(data + skip, buffer + buffer_pos, chunk);
  438. err = reiserfs_commit_write(NULL, page, page_offset,
  439. page_offset + chunk +
  440. skip);
  441. }
  442. unlock_page(page);
  443. reiserfs_put_page(page);
  444. buffer_pos += chunk;
  445. file_pos += chunk;
  446. skip = 0;
  447. if (err || buffer_size == 0 || !buffer)
  448. break;
  449. }
  450. /* We can't mark the inode dirty if it's not hashed. This is the case
  451. * when we're inheriting the default ACL. If we dirty it, the inode
  452. * gets marked dirty, but won't (ever) make it onto the dirty list until
  453. * it's synced explicitly to clear I_DIRTY. This is bad. */
  454. if (!hlist_unhashed(&inode->i_hash)) {
  455. inode->i_ctime = CURRENT_TIME_SEC;
  456. mark_inode_dirty(inode);
  457. }
  458. out_filp:
  459. mutex_unlock(&xinode->i_mutex);
  460. dput(dentry);
  461. out:
  462. return err;
  463. }
  464. /*
  465. * inode->i_mutex: down
  466. */
  467. int
  468. reiserfs_xattr_get(const struct inode *inode, const char *name, void *buffer,
  469. size_t buffer_size)
  470. {
  471. ssize_t err = 0;
  472. struct dentry *dentry;
  473. size_t isize;
  474. size_t file_pos = 0;
  475. size_t buffer_pos = 0;
  476. struct page *page;
  477. struct inode *xinode;
  478. __u32 hash = 0;
  479. if (name == NULL)
  480. return -EINVAL;
  481. /* We can't have xattrs attached to v1 items since they don't have
  482. * generation numbers */
  483. if (get_inode_sd_version(inode) == STAT_DATA_V1)
  484. return -EOPNOTSUPP;
  485. dentry = get_xa_file_dentry(inode, name, FL_READONLY);
  486. if (IS_ERR(dentry)) {
  487. err = PTR_ERR(dentry);
  488. goto out;
  489. }
  490. xinode = dentry->d_inode;
  491. isize = xinode->i_size;
  492. REISERFS_I(inode)->i_flags |= i_has_xattr_dir;
  493. /* Just return the size needed */
  494. if (buffer == NULL) {
  495. err = isize - sizeof(struct reiserfs_xattr_header);
  496. goto out_dput;
  497. }
  498. if (buffer_size < isize - sizeof(struct reiserfs_xattr_header)) {
  499. err = -ERANGE;
  500. goto out_dput;
  501. }
  502. while (file_pos < isize) {
  503. size_t chunk;
  504. char *data;
  505. size_t skip = 0;
  506. if (isize - file_pos > PAGE_CACHE_SIZE)
  507. chunk = PAGE_CACHE_SIZE;
  508. else
  509. chunk = isize - file_pos;
  510. page = reiserfs_get_page(xinode, file_pos >> PAGE_CACHE_SHIFT);
  511. if (IS_ERR(page)) {
  512. err = PTR_ERR(page);
  513. goto out_dput;
  514. }
  515. lock_page(page);
  516. data = page_address(page);
  517. if (file_pos == 0) {
  518. struct reiserfs_xattr_header *rxh =
  519. (struct reiserfs_xattr_header *)data;
  520. skip = file_pos = sizeof(struct reiserfs_xattr_header);
  521. chunk -= skip;
  522. /* Magic doesn't match up.. */
  523. if (rxh->h_magic != cpu_to_le32(REISERFS_XATTR_MAGIC)) {
  524. unlock_page(page);
  525. reiserfs_put_page(page);
  526. reiserfs_warning(inode->i_sb,
  527. "Invalid magic for xattr (%s) "
  528. "associated with %k", name,
  529. INODE_PKEY(inode));
  530. err = -EIO;
  531. goto out_dput;
  532. }
  533. hash = le32_to_cpu(rxh->h_hash);
  534. }
  535. memcpy(buffer + buffer_pos, data + skip, chunk);
  536. unlock_page(page);
  537. reiserfs_put_page(page);
  538. file_pos += chunk;
  539. buffer_pos += chunk;
  540. skip = 0;
  541. }
  542. err = isize - sizeof(struct reiserfs_xattr_header);
  543. if (xattr_hash(buffer, isize - sizeof(struct reiserfs_xattr_header)) !=
  544. hash) {
  545. reiserfs_warning(inode->i_sb,
  546. "Invalid hash for xattr (%s) associated "
  547. "with %k", name, INODE_PKEY(inode));
  548. err = -EIO;
  549. }
  550. out_dput:
  551. dput(dentry);
  552. out:
  553. return err;
  554. }
  555. static int
  556. __reiserfs_xattr_del(struct dentry *xadir, const char *name, int namelen)
  557. {
  558. struct dentry *dentry;
  559. struct inode *dir = xadir->d_inode;
  560. int err = 0;
  561. dentry = lookup_one_len(name, xadir, namelen);
  562. if (IS_ERR(dentry)) {
  563. err = PTR_ERR(dentry);
  564. goto out;
  565. } else if (!dentry->d_inode) {
  566. err = -ENODATA;
  567. goto out_file;
  568. }
  569. /* Skip directories.. */
  570. if (S_ISDIR(dentry->d_inode->i_mode))
  571. goto out_file;
  572. if (!is_reiserfs_priv_object(dentry->d_inode)) {
  573. reiserfs_warning(dir->i_sb, "OID %08x [%.*s/%.*s] doesn't have "
  574. "priv flag set [parent is %sset].",
  575. le32_to_cpu(INODE_PKEY(dentry->d_inode)->
  576. k_objectid), xadir->d_name.len,
  577. xadir->d_name.name, namelen, name,
  578. is_reiserfs_priv_object(xadir->
  579. d_inode) ? "" :
  580. "not ");
  581. dput(dentry);
  582. return -EIO;
  583. }
  584. err = dir->i_op->unlink(dir, dentry);
  585. if (!err)
  586. d_delete(dentry);
  587. out_file:
  588. dput(dentry);
  589. out:
  590. return err;
  591. }
  592. int reiserfs_xattr_del(struct inode *inode, const char *name)
  593. {
  594. struct dentry *dir;
  595. int err;
  596. dir = open_xa_dir(inode, FL_READONLY);
  597. if (IS_ERR(dir)) {
  598. err = PTR_ERR(dir);
  599. goto out;
  600. }
  601. err = __reiserfs_xattr_del(dir, name, strlen(name));
  602. dput(dir);
  603. if (!err) {
  604. inode->i_ctime = CURRENT_TIME_SEC;
  605. mark_inode_dirty(inode);
  606. }
  607. out:
  608. return err;
  609. }
  610. /* The following are side effects of other operations that aren't explicitly
  611. * modifying extended attributes. This includes operations such as permissions
  612. * or ownership changes, object deletions, etc. */
  613. static int
  614. reiserfs_delete_xattrs_filler(void *buf, const char *name, int namelen,
  615. loff_t offset, u64 ino, unsigned int d_type)
  616. {
  617. struct dentry *xadir = (struct dentry *)buf;
  618. return __reiserfs_xattr_del(xadir, name, namelen);
  619. }
  620. /* This is called w/ inode->i_mutex downed */
  621. int reiserfs_delete_xattrs(struct inode *inode)
  622. {
  623. struct dentry *dir, *root;
  624. int err = 0;
  625. /* Skip out, an xattr has no xattrs associated with it */
  626. if (is_reiserfs_priv_object(inode) ||
  627. get_inode_sd_version(inode) == STAT_DATA_V1 ||
  628. !reiserfs_xattrs(inode->i_sb)) {
  629. return 0;
  630. }
  631. reiserfs_read_lock_xattrs(inode->i_sb);
  632. dir = open_xa_dir(inode, FL_READONLY);
  633. reiserfs_read_unlock_xattrs(inode->i_sb);
  634. if (IS_ERR(dir)) {
  635. err = PTR_ERR(dir);
  636. goto out;
  637. } else if (!dir->d_inode) {
  638. dput(dir);
  639. return 0;
  640. }
  641. lock_kernel();
  642. err = xattr_readdir(dir->d_inode, reiserfs_delete_xattrs_filler, dir);
  643. if (err) {
  644. unlock_kernel();
  645. goto out_dir;
  646. }
  647. /* Leftovers besides . and .. -- that's not good. */
  648. if (dir->d_inode->i_nlink <= 2) {
  649. root = get_xa_root(inode->i_sb, XATTR_REPLACE);
  650. reiserfs_write_lock_xattrs(inode->i_sb);
  651. err = vfs_rmdir(root->d_inode, dir);
  652. reiserfs_write_unlock_xattrs(inode->i_sb);
  653. dput(root);
  654. } else {
  655. reiserfs_warning(inode->i_sb,
  656. "Couldn't remove all entries in directory");
  657. }
  658. unlock_kernel();
  659. out_dir:
  660. dput(dir);
  661. out:
  662. if (!err)
  663. REISERFS_I(inode)->i_flags =
  664. REISERFS_I(inode)->i_flags & ~i_has_xattr_dir;
  665. return err;
  666. }
  667. struct reiserfs_chown_buf {
  668. struct inode *inode;
  669. struct dentry *xadir;
  670. struct iattr *attrs;
  671. };
  672. /* XXX: If there is a better way to do this, I'd love to hear about it */
  673. static int
  674. reiserfs_chown_xattrs_filler(void *buf, const char *name, int namelen,
  675. loff_t offset, u64 ino, unsigned int d_type)
  676. {
  677. struct reiserfs_chown_buf *chown_buf = (struct reiserfs_chown_buf *)buf;
  678. struct dentry *xafile, *xadir = chown_buf->xadir;
  679. struct iattr *attrs = chown_buf->attrs;
  680. int err = 0;
  681. xafile = lookup_one_len(name, xadir, namelen);
  682. if (IS_ERR(xafile))
  683. return PTR_ERR(xafile);
  684. else if (!xafile->d_inode) {
  685. dput(xafile);
  686. return -ENODATA;
  687. }
  688. if (!S_ISDIR(xafile->d_inode->i_mode))
  689. err = notify_change(xafile, attrs);
  690. dput(xafile);
  691. return err;
  692. }
  693. int reiserfs_chown_xattrs(struct inode *inode, struct iattr *attrs)
  694. {
  695. struct dentry *dir;
  696. int err = 0;
  697. struct reiserfs_chown_buf buf;
  698. unsigned int ia_valid = attrs->ia_valid;
  699. /* Skip out, an xattr has no xattrs associated with it */
  700. if (is_reiserfs_priv_object(inode) ||
  701. get_inode_sd_version(inode) == STAT_DATA_V1 ||
  702. !reiserfs_xattrs(inode->i_sb)) {
  703. return 0;
  704. }
  705. reiserfs_read_lock_xattrs(inode->i_sb);
  706. dir = open_xa_dir(inode, FL_READONLY);
  707. reiserfs_read_unlock_xattrs(inode->i_sb);
  708. if (IS_ERR(dir)) {
  709. if (PTR_ERR(dir) != -ENODATA)
  710. err = PTR_ERR(dir);
  711. goto out;
  712. } else if (!dir->d_inode) {
  713. dput(dir);
  714. goto out;
  715. }
  716. lock_kernel();
  717. attrs->ia_valid &= (ATTR_UID | ATTR_GID | ATTR_CTIME);
  718. buf.xadir = dir;
  719. buf.attrs = attrs;
  720. buf.inode = inode;
  721. err = xattr_readdir(dir->d_inode, reiserfs_chown_xattrs_filler, &buf);
  722. if (err) {
  723. unlock_kernel();
  724. goto out_dir;
  725. }
  726. err = notify_change(dir, attrs);
  727. unlock_kernel();
  728. out_dir:
  729. dput(dir);
  730. out:
  731. attrs->ia_valid = ia_valid;
  732. return err;
  733. }
  734. /* Actual operations that are exported to VFS-land */
  735. /*
  736. * Inode operation getxattr()
  737. * Preliminary locking: we down dentry->d_inode->i_mutex
  738. */
  739. ssize_t
  740. reiserfs_getxattr(struct dentry * dentry, const char *name, void *buffer,
  741. size_t size)
  742. {
  743. struct reiserfs_xattr_handler *xah = find_xattr_handler_prefix(name);
  744. int err;
  745. if (!xah || !reiserfs_xattrs(dentry->d_sb) ||
  746. get_inode_sd_version(dentry->d_inode) == STAT_DATA_V1)
  747. return -EOPNOTSUPP;
  748. reiserfs_read_lock_xattr_i(dentry->d_inode);
  749. reiserfs_read_lock_xattrs(dentry->d_sb);
  750. err = xah->get(dentry->d_inode, name, buffer, size);
  751. reiserfs_read_unlock_xattrs(dentry->d_sb);
  752. reiserfs_read_unlock_xattr_i(dentry->d_inode);
  753. return err;
  754. }
  755. /*
  756. * Inode operation setxattr()
  757. *
  758. * dentry->d_inode->i_mutex down
  759. */
  760. int
  761. reiserfs_setxattr(struct dentry *dentry, const char *name, const void *value,
  762. size_t size, int flags)
  763. {
  764. struct reiserfs_xattr_handler *xah = find_xattr_handler_prefix(name);
  765. int err;
  766. int lock;
  767. if (!xah || !reiserfs_xattrs(dentry->d_sb) ||
  768. get_inode_sd_version(dentry->d_inode) == STAT_DATA_V1)
  769. return -EOPNOTSUPP;
  770. reiserfs_write_lock_xattr_i(dentry->d_inode);
  771. lock = !has_xattr_dir(dentry->d_inode);
  772. if (lock)
  773. reiserfs_write_lock_xattrs(dentry->d_sb);
  774. else
  775. reiserfs_read_lock_xattrs(dentry->d_sb);
  776. err = xah->set(dentry->d_inode, name, value, size, flags);
  777. if (lock)
  778. reiserfs_write_unlock_xattrs(dentry->d_sb);
  779. else
  780. reiserfs_read_unlock_xattrs(dentry->d_sb);
  781. reiserfs_write_unlock_xattr_i(dentry->d_inode);
  782. return err;
  783. }
  784. /*
  785. * Inode operation removexattr()
  786. *
  787. * dentry->d_inode->i_mutex down
  788. */
  789. int reiserfs_removexattr(struct dentry *dentry, const char *name)
  790. {
  791. int err;
  792. struct reiserfs_xattr_handler *xah = find_xattr_handler_prefix(name);
  793. if (!xah || !reiserfs_xattrs(dentry->d_sb) ||
  794. get_inode_sd_version(dentry->d_inode) == STAT_DATA_V1)
  795. return -EOPNOTSUPP;
  796. reiserfs_write_lock_xattr_i(dentry->d_inode);
  797. reiserfs_read_lock_xattrs(dentry->d_sb);
  798. /* Deletion pre-operation */
  799. if (xah->del) {
  800. err = xah->del(dentry->d_inode, name);
  801. if (err)
  802. goto out;
  803. }
  804. err = reiserfs_xattr_del(dentry->d_inode, name);
  805. dentry->d_inode->i_ctime = CURRENT_TIME_SEC;
  806. mark_inode_dirty(dentry->d_inode);
  807. out:
  808. reiserfs_read_unlock_xattrs(dentry->d_sb);
  809. reiserfs_write_unlock_xattr_i(dentry->d_inode);
  810. return err;
  811. }
  812. /* This is what filldir will use:
  813. * r_pos will always contain the amount of space required for the entire
  814. * list. If r_pos becomes larger than r_size, we need more space and we
  815. * return an error indicating this. If r_pos is less than r_size, then we've
  816. * filled the buffer successfully and we return success */
  817. struct reiserfs_listxattr_buf {
  818. int r_pos;
  819. int r_size;
  820. char *r_buf;
  821. struct inode *r_inode;
  822. };
  823. static int
  824. reiserfs_listxattr_filler(void *buf, const char *name, int namelen,
  825. loff_t offset, u64 ino, unsigned int d_type)
  826. {
  827. struct reiserfs_listxattr_buf *b = (struct reiserfs_listxattr_buf *)buf;
  828. int len = 0;
  829. if (name[0] != '.'
  830. || (namelen != 1 && (name[1] != '.' || namelen != 2))) {
  831. struct reiserfs_xattr_handler *xah =
  832. find_xattr_handler_prefix(name);
  833. if (!xah)
  834. return 0; /* Unsupported xattr name, skip it */
  835. /* We call ->list() twice because the operation isn't required to just
  836. * return the name back - we want to make sure we have enough space */
  837. len += xah->list(b->r_inode, name, namelen, NULL);
  838. if (len) {
  839. if (b->r_pos + len + 1 <= b->r_size) {
  840. char *p = b->r_buf + b->r_pos;
  841. p += xah->list(b->r_inode, name, namelen, p);
  842. *p++ = '\0';
  843. }
  844. b->r_pos += len + 1;
  845. }
  846. }
  847. return 0;
  848. }
  849. /*
  850. * Inode operation listxattr()
  851. *
  852. * Preliminary locking: we down dentry->d_inode->i_mutex
  853. */
  854. ssize_t reiserfs_listxattr(struct dentry * dentry, char *buffer, size_t size)
  855. {
  856. struct dentry *dir;
  857. int err = 0;
  858. struct reiserfs_listxattr_buf buf;
  859. if (!dentry->d_inode)
  860. return -EINVAL;
  861. if (!reiserfs_xattrs(dentry->d_sb) ||
  862. get_inode_sd_version(dentry->d_inode) == STAT_DATA_V1)
  863. return -EOPNOTSUPP;
  864. reiserfs_read_lock_xattr_i(dentry->d_inode);
  865. reiserfs_read_lock_xattrs(dentry->d_sb);
  866. dir = open_xa_dir(dentry->d_inode, FL_READONLY);
  867. reiserfs_read_unlock_xattrs(dentry->d_sb);
  868. if (IS_ERR(dir)) {
  869. err = PTR_ERR(dir);
  870. if (err == -ENODATA)
  871. err = 0; /* Not an error if there aren't any xattrs */
  872. goto out;
  873. }
  874. buf.r_buf = buffer;
  875. buf.r_size = buffer ? size : 0;
  876. buf.r_pos = 0;
  877. buf.r_inode = dentry->d_inode;
  878. REISERFS_I(dentry->d_inode)->i_flags |= i_has_xattr_dir;
  879. err = xattr_readdir(dir->d_inode, reiserfs_listxattr_filler, &buf);
  880. if (err)
  881. goto out_dir;
  882. if (buf.r_pos > buf.r_size && buffer != NULL)
  883. err = -ERANGE;
  884. else
  885. err = buf.r_pos;
  886. out_dir:
  887. dput(dir);
  888. out:
  889. reiserfs_read_unlock_xattr_i(dentry->d_inode);
  890. return err;
  891. }
  892. /* This is the implementation for the xattr plugin infrastructure */
  893. static LIST_HEAD(xattr_handlers);
  894. static DEFINE_RWLOCK(handler_lock);
  895. static struct reiserfs_xattr_handler *find_xattr_handler_prefix(const char
  896. *prefix)
  897. {
  898. struct reiserfs_xattr_handler *xah = NULL;
  899. struct list_head *p;
  900. read_lock(&handler_lock);
  901. list_for_each(p, &xattr_handlers) {
  902. xah = list_entry(p, struct reiserfs_xattr_handler, handlers);
  903. if (strncmp(xah->prefix, prefix, strlen(xah->prefix)) == 0)
  904. break;
  905. xah = NULL;
  906. }
  907. read_unlock(&handler_lock);
  908. return xah;
  909. }
  910. static void __unregister_handlers(void)
  911. {
  912. struct reiserfs_xattr_handler *xah;
  913. struct list_head *p, *tmp;
  914. list_for_each_safe(p, tmp, &xattr_handlers) {
  915. xah = list_entry(p, struct reiserfs_xattr_handler, handlers);
  916. if (xah->exit)
  917. xah->exit();
  918. list_del_init(p);
  919. }
  920. INIT_LIST_HEAD(&xattr_handlers);
  921. }
  922. int __init reiserfs_xattr_register_handlers(void)
  923. {
  924. int err = 0;
  925. struct reiserfs_xattr_handler *xah;
  926. struct list_head *p;
  927. write_lock(&handler_lock);
  928. /* If we're already initialized, nothing to do */
  929. if (!list_empty(&xattr_handlers)) {
  930. write_unlock(&handler_lock);
  931. return 0;
  932. }
  933. /* Add the handlers */
  934. list_add_tail(&user_handler.handlers, &xattr_handlers);
  935. list_add_tail(&trusted_handler.handlers, &xattr_handlers);
  936. #ifdef CONFIG_REISERFS_FS_SECURITY
  937. list_add_tail(&security_handler.handlers, &xattr_handlers);
  938. #endif
  939. #ifdef CONFIG_REISERFS_FS_POSIX_ACL
  940. list_add_tail(&posix_acl_access_handler.handlers, &xattr_handlers);
  941. list_add_tail(&posix_acl_default_handler.handlers, &xattr_handlers);
  942. #endif
  943. /* Run initializers, if available */
  944. list_for_each(p, &xattr_handlers) {
  945. xah = list_entry(p, struct reiserfs_xattr_handler, handlers);
  946. if (xah->init) {
  947. err = xah->init();
  948. if (err) {
  949. list_del_init(p);
  950. break;
  951. }
  952. }
  953. }
  954. /* Clean up other handlers, if any failed */
  955. if (err)
  956. __unregister_handlers();
  957. write_unlock(&handler_lock);
  958. return err;
  959. }
  960. void reiserfs_xattr_unregister_handlers(void)
  961. {
  962. write_lock(&handler_lock);
  963. __unregister_handlers();
  964. write_unlock(&handler_lock);
  965. }
  966. /* This will catch lookups from the fs root to .reiserfs_priv */
  967. static int
  968. xattr_lookup_poison(struct dentry *dentry, struct qstr *q1, struct qstr *name)
  969. {
  970. struct dentry *priv_root = REISERFS_SB(dentry->d_sb)->priv_root;
  971. if (name->len == priv_root->d_name.len &&
  972. name->hash == priv_root->d_name.hash &&
  973. !memcmp(name->name, priv_root->d_name.name, name->len)) {
  974. return -ENOENT;
  975. } else if (q1->len == name->len &&
  976. !memcmp(q1->name, name->name, name->len))
  977. return 0;
  978. return 1;
  979. }
  980. static struct dentry_operations xattr_lookup_poison_ops = {
  981. .d_compare = xattr_lookup_poison,
  982. };
  983. /* We need to take a copy of the mount flags since things like
  984. * MS_RDONLY don't get set until *after* we're called.
  985. * mount_flags != mount_options */
  986. int reiserfs_xattr_init(struct super_block *s, int mount_flags)
  987. {
  988. int err = 0;
  989. /* We need generation numbers to ensure that the oid mapping is correct
  990. * v3.5 filesystems don't have them. */
  991. if (!old_format_only(s)) {
  992. set_bit(REISERFS_XATTRS, &(REISERFS_SB(s)->s_mount_opt));
  993. } else if (reiserfs_xattrs_optional(s)) {
  994. /* Old format filesystem, but optional xattrs have been enabled
  995. * at mount time. Error out. */
  996. reiserfs_warning(s, "xattrs/ACLs not supported on pre v3.6 "
  997. "format filesystem. Failing mount.");
  998. err = -EOPNOTSUPP;
  999. goto error;
  1000. } else {
  1001. /* Old format filesystem, but no optional xattrs have been enabled. This
  1002. * means we silently disable xattrs on the filesystem. */
  1003. clear_bit(REISERFS_XATTRS, &(REISERFS_SB(s)->s_mount_opt));
  1004. }
  1005. /* If we don't have the privroot located yet - go find it */
  1006. if (reiserfs_xattrs(s) && !REISERFS_SB(s)->priv_root) {
  1007. struct dentry *dentry;
  1008. dentry = lookup_one_len(PRIVROOT_NAME, s->s_root,
  1009. strlen(PRIVROOT_NAME));
  1010. if (!IS_ERR(dentry)) {
  1011. if (!(mount_flags & MS_RDONLY) && !dentry->d_inode) {
  1012. struct inode *inode = dentry->d_parent->d_inode;
  1013. mutex_lock_nested(&inode->i_mutex,
  1014. I_MUTEX_XATTR);
  1015. err = inode->i_op->mkdir(inode, dentry, 0700);
  1016. mutex_unlock(&inode->i_mutex);
  1017. if (err) {
  1018. dput(dentry);
  1019. dentry = NULL;
  1020. }
  1021. if (dentry && dentry->d_inode)
  1022. reiserfs_warning(s,
  1023. "Created %s on %s - reserved for "
  1024. "xattr storage.",
  1025. PRIVROOT_NAME,
  1026. reiserfs_bdevname
  1027. (inode->i_sb));
  1028. } else if (!dentry->d_inode) {
  1029. dput(dentry);
  1030. dentry = NULL;
  1031. }
  1032. } else
  1033. err = PTR_ERR(dentry);
  1034. if (!err && dentry) {
  1035. s->s_root->d_op = &xattr_lookup_poison_ops;
  1036. reiserfs_mark_inode_private(dentry->d_inode);
  1037. REISERFS_SB(s)->priv_root = dentry;
  1038. } else if (!(mount_flags & MS_RDONLY)) { /* xattrs are unavailable */
  1039. /* If we're read-only it just means that the dir hasn't been
  1040. * created. Not an error -- just no xattrs on the fs. We'll
  1041. * check again if we go read-write */
  1042. reiserfs_warning(s, "xattrs/ACLs enabled and couldn't "
  1043. "find/create .reiserfs_priv. Failing mount.");
  1044. err = -EOPNOTSUPP;
  1045. }
  1046. }
  1047. error:
  1048. /* This is only nonzero if there was an error initializing the xattr
  1049. * directory or if there is a condition where we don't support them. */
  1050. if (err) {
  1051. clear_bit(REISERFS_XATTRS, &(REISERFS_SB(s)->s_mount_opt));
  1052. clear_bit(REISERFS_XATTRS_USER, &(REISERFS_SB(s)->s_mount_opt));
  1053. clear_bit(REISERFS_POSIXACL, &(REISERFS_SB(s)->s_mount_opt));
  1054. }
  1055. /* The super_block MS_POSIXACL must mirror the (no)acl mount option. */
  1056. s->s_flags = s->s_flags & ~MS_POSIXACL;
  1057. if (reiserfs_posixacl(s))
  1058. s->s_flags |= MS_POSIXACL;
  1059. return err;
  1060. }
  1061. static int reiserfs_check_acl(struct inode *inode, int mask)
  1062. {
  1063. struct posix_acl *acl;
  1064. int error = -EAGAIN; /* do regular unix permission checks by default */
  1065. reiserfs_read_lock_xattr_i(inode);
  1066. reiserfs_read_lock_xattrs(inode->i_sb);
  1067. acl = reiserfs_get_acl(inode, ACL_TYPE_ACCESS);
  1068. reiserfs_read_unlock_xattrs(inode->i_sb);
  1069. reiserfs_read_unlock_xattr_i(inode);
  1070. if (acl) {
  1071. if (!IS_ERR(acl)) {
  1072. error = posix_acl_permission(inode, acl, mask);
  1073. posix_acl_release(acl);
  1074. } else if (PTR_ERR(acl) != -ENODATA)
  1075. error = PTR_ERR(acl);
  1076. }
  1077. return error;
  1078. }
  1079. int reiserfs_permission(struct inode *inode, int mask)
  1080. {
  1081. /*
  1082. * We don't do permission checks on the internal objects.
  1083. * Permissions are determined by the "owning" object.
  1084. */
  1085. if (is_reiserfs_priv_object(inode))
  1086. return 0;
  1087. /*
  1088. * Stat data v1 doesn't support ACLs.
  1089. */
  1090. if (get_inode_sd_version(inode) == STAT_DATA_V1)
  1091. return generic_permission(inode, mask, NULL);
  1092. else
  1093. return generic_permission(inode, mask, reiserfs_check_acl);
  1094. }