xattr.c 36 KB

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