ops_inode.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438
  1. /*
  2. * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
  3. * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved.
  4. *
  5. * This copyrighted material is made available to anyone wishing to use,
  6. * modify, copy, or redistribute it subject to the terms and conditions
  7. * of the GNU General Public License version 2.
  8. */
  9. #include <linux/slab.h>
  10. #include <linux/spinlock.h>
  11. #include <linux/completion.h>
  12. #include <linux/buffer_head.h>
  13. #include <linux/namei.h>
  14. #include <linux/utsname.h>
  15. #include <linux/mm.h>
  16. #include <linux/xattr.h>
  17. #include <linux/posix_acl.h>
  18. #include <linux/gfs2_ondisk.h>
  19. #include <linux/crc32.h>
  20. #include <linux/fiemap.h>
  21. #include <asm/uaccess.h>
  22. #include "gfs2.h"
  23. #include "incore.h"
  24. #include "acl.h"
  25. #include "bmap.h"
  26. #include "dir.h"
  27. #include "xattr.h"
  28. #include "glock.h"
  29. #include "inode.h"
  30. #include "meta_io.h"
  31. #include "quota.h"
  32. #include "rgrp.h"
  33. #include "trans.h"
  34. #include "util.h"
  35. #include "super.h"
  36. /**
  37. * gfs2_create - Create a file
  38. * @dir: The directory in which to create the file
  39. * @dentry: The dentry of the new file
  40. * @mode: The mode of the new file
  41. *
  42. * Returns: errno
  43. */
  44. static int gfs2_create(struct inode *dir, struct dentry *dentry,
  45. int mode, struct nameidata *nd)
  46. {
  47. struct gfs2_inode *dip = GFS2_I(dir);
  48. struct gfs2_sbd *sdp = GFS2_SB(dir);
  49. struct gfs2_holder ghs[2];
  50. struct inode *inode;
  51. gfs2_holder_init(dip->i_gl, 0, 0, ghs);
  52. for (;;) {
  53. inode = gfs2_createi(ghs, &dentry->d_name, S_IFREG | mode, 0);
  54. if (!IS_ERR(inode)) {
  55. gfs2_trans_end(sdp);
  56. if (dip->i_alloc->al_rgd)
  57. gfs2_inplace_release(dip);
  58. gfs2_quota_unlock(dip);
  59. gfs2_alloc_put(dip);
  60. gfs2_glock_dq_uninit_m(2, ghs);
  61. mark_inode_dirty(inode);
  62. break;
  63. } else if (PTR_ERR(inode) != -EEXIST ||
  64. (nd && nd->flags & LOOKUP_EXCL)) {
  65. gfs2_holder_uninit(ghs);
  66. return PTR_ERR(inode);
  67. }
  68. inode = gfs2_lookupi(dir, &dentry->d_name, 0);
  69. if (inode) {
  70. if (!IS_ERR(inode)) {
  71. gfs2_holder_uninit(ghs);
  72. break;
  73. } else {
  74. gfs2_holder_uninit(ghs);
  75. return PTR_ERR(inode);
  76. }
  77. }
  78. }
  79. d_instantiate(dentry, inode);
  80. return 0;
  81. }
  82. /**
  83. * gfs2_lookup - Look up a filename in a directory and return its inode
  84. * @dir: The directory inode
  85. * @dentry: The dentry of the new inode
  86. * @nd: passed from Linux VFS, ignored by us
  87. *
  88. * Called by the VFS layer. Lock dir and call gfs2_lookupi()
  89. *
  90. * Returns: errno
  91. */
  92. static struct dentry *gfs2_lookup(struct inode *dir, struct dentry *dentry,
  93. struct nameidata *nd)
  94. {
  95. struct inode *inode = NULL;
  96. dentry->d_op = &gfs2_dops;
  97. inode = gfs2_lookupi(dir, &dentry->d_name, 0);
  98. if (inode && IS_ERR(inode))
  99. return ERR_CAST(inode);
  100. if (inode) {
  101. struct gfs2_glock *gl = GFS2_I(inode)->i_gl;
  102. struct gfs2_holder gh;
  103. int error;
  104. error = gfs2_glock_nq_init(gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
  105. if (error) {
  106. iput(inode);
  107. return ERR_PTR(error);
  108. }
  109. gfs2_glock_dq_uninit(&gh);
  110. return d_splice_alias(inode, dentry);
  111. }
  112. d_add(dentry, inode);
  113. return NULL;
  114. }
  115. /**
  116. * gfs2_link - Link to a file
  117. * @old_dentry: The inode to link
  118. * @dir: Add link to this directory
  119. * @dentry: The name of the link
  120. *
  121. * Link the inode in "old_dentry" into the directory "dir" with the
  122. * name in "dentry".
  123. *
  124. * Returns: errno
  125. */
  126. static int gfs2_link(struct dentry *old_dentry, struct inode *dir,
  127. struct dentry *dentry)
  128. {
  129. struct gfs2_inode *dip = GFS2_I(dir);
  130. struct gfs2_sbd *sdp = GFS2_SB(dir);
  131. struct inode *inode = old_dentry->d_inode;
  132. struct gfs2_inode *ip = GFS2_I(inode);
  133. struct gfs2_holder ghs[2];
  134. int alloc_required;
  135. int error;
  136. if (S_ISDIR(inode->i_mode))
  137. return -EPERM;
  138. gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
  139. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
  140. error = gfs2_glock_nq(ghs); /* parent */
  141. if (error)
  142. goto out_parent;
  143. error = gfs2_glock_nq(ghs + 1); /* child */
  144. if (error)
  145. goto out_child;
  146. error = gfs2_permission(dir, MAY_WRITE | MAY_EXEC);
  147. if (error)
  148. goto out_gunlock;
  149. error = gfs2_dir_check(dir, &dentry->d_name, NULL);
  150. switch (error) {
  151. case -ENOENT:
  152. break;
  153. case 0:
  154. error = -EEXIST;
  155. default:
  156. goto out_gunlock;
  157. }
  158. error = -EINVAL;
  159. if (!dip->i_inode.i_nlink)
  160. goto out_gunlock;
  161. error = -EFBIG;
  162. if (dip->i_entries == (u32)-1)
  163. goto out_gunlock;
  164. error = -EPERM;
  165. if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
  166. goto out_gunlock;
  167. error = -EINVAL;
  168. if (!ip->i_inode.i_nlink)
  169. goto out_gunlock;
  170. error = -EMLINK;
  171. if (ip->i_inode.i_nlink == (u32)-1)
  172. goto out_gunlock;
  173. alloc_required = error = gfs2_diradd_alloc_required(dir, &dentry->d_name);
  174. if (error < 0)
  175. goto out_gunlock;
  176. error = 0;
  177. if (alloc_required) {
  178. struct gfs2_alloc *al = gfs2_alloc_get(dip);
  179. if (!al) {
  180. error = -ENOMEM;
  181. goto out_gunlock;
  182. }
  183. error = gfs2_quota_lock_check(dip);
  184. if (error)
  185. goto out_alloc;
  186. al->al_requested = sdp->sd_max_dirres;
  187. error = gfs2_inplace_reserve(dip);
  188. if (error)
  189. goto out_gunlock_q;
  190. error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
  191. al->al_rgd->rd_length +
  192. 2 * RES_DINODE + RES_STATFS +
  193. RES_QUOTA, 0);
  194. if (error)
  195. goto out_ipres;
  196. } else {
  197. error = gfs2_trans_begin(sdp, 2 * RES_DINODE + RES_LEAF, 0);
  198. if (error)
  199. goto out_ipres;
  200. }
  201. error = gfs2_dir_add(dir, &dentry->d_name, ip, IF2DT(inode->i_mode));
  202. if (error)
  203. goto out_end_trans;
  204. error = gfs2_change_nlink(ip, +1);
  205. out_end_trans:
  206. gfs2_trans_end(sdp);
  207. out_ipres:
  208. if (alloc_required)
  209. gfs2_inplace_release(dip);
  210. out_gunlock_q:
  211. if (alloc_required)
  212. gfs2_quota_unlock(dip);
  213. out_alloc:
  214. if (alloc_required)
  215. gfs2_alloc_put(dip);
  216. out_gunlock:
  217. gfs2_glock_dq(ghs + 1);
  218. out_child:
  219. gfs2_glock_dq(ghs);
  220. out_parent:
  221. gfs2_holder_uninit(ghs);
  222. gfs2_holder_uninit(ghs + 1);
  223. if (!error) {
  224. atomic_inc(&inode->i_count);
  225. d_instantiate(dentry, inode);
  226. mark_inode_dirty(inode);
  227. }
  228. return error;
  229. }
  230. /*
  231. * gfs2_unlink_ok - check to see that a inode is still in a directory
  232. * @dip: the directory
  233. * @name: the name of the file
  234. * @ip: the inode
  235. *
  236. * Assumes that the lock on (at least) @dip is held.
  237. *
  238. * Returns: 0 if the parent/child relationship is correct, errno if it isn't
  239. */
  240. static int gfs2_unlink_ok(struct gfs2_inode *dip, const struct qstr *name,
  241. const struct gfs2_inode *ip)
  242. {
  243. int error;
  244. if (IS_IMMUTABLE(&ip->i_inode) || IS_APPEND(&ip->i_inode))
  245. return -EPERM;
  246. if ((dip->i_inode.i_mode & S_ISVTX) &&
  247. dip->i_inode.i_uid != current_fsuid() &&
  248. ip->i_inode.i_uid != current_fsuid() && !capable(CAP_FOWNER))
  249. return -EPERM;
  250. if (IS_APPEND(&dip->i_inode))
  251. return -EPERM;
  252. error = gfs2_permission(&dip->i_inode, MAY_WRITE | MAY_EXEC);
  253. if (error)
  254. return error;
  255. error = gfs2_dir_check(&dip->i_inode, name, ip);
  256. if (error)
  257. return error;
  258. return 0;
  259. }
  260. /**
  261. * gfs2_unlink - Unlink a file
  262. * @dir: The inode of the directory containing the file to unlink
  263. * @dentry: The file itself
  264. *
  265. * Unlink a file. Call gfs2_unlinki()
  266. *
  267. * Returns: errno
  268. */
  269. static int gfs2_unlink(struct inode *dir, struct dentry *dentry)
  270. {
  271. struct gfs2_inode *dip = GFS2_I(dir);
  272. struct gfs2_sbd *sdp = GFS2_SB(dir);
  273. struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
  274. struct gfs2_holder ghs[3];
  275. struct gfs2_rgrpd *rgd;
  276. struct gfs2_holder ri_gh;
  277. int error;
  278. error = gfs2_rindex_hold(sdp, &ri_gh);
  279. if (error)
  280. return error;
  281. gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
  282. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
  283. rgd = gfs2_blk2rgrpd(sdp, ip->i_no_addr);
  284. gfs2_holder_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + 2);
  285. error = gfs2_glock_nq(ghs); /* parent */
  286. if (error)
  287. goto out_parent;
  288. error = gfs2_glock_nq(ghs + 1); /* child */
  289. if (error)
  290. goto out_child;
  291. error = gfs2_glock_nq(ghs + 2); /* rgrp */
  292. if (error)
  293. goto out_rgrp;
  294. error = gfs2_unlink_ok(dip, &dentry->d_name, ip);
  295. if (error)
  296. goto out_gunlock;
  297. error = gfs2_trans_begin(sdp, 2*RES_DINODE + RES_LEAF + RES_RG_BIT, 0);
  298. if (error)
  299. goto out_gunlock;
  300. error = gfs2_dir_del(dip, &dentry->d_name);
  301. if (error)
  302. goto out_end_trans;
  303. error = gfs2_change_nlink(ip, -1);
  304. out_end_trans:
  305. gfs2_trans_end(sdp);
  306. out_gunlock:
  307. gfs2_glock_dq(ghs + 2);
  308. out_rgrp:
  309. gfs2_holder_uninit(ghs + 2);
  310. gfs2_glock_dq(ghs + 1);
  311. out_child:
  312. gfs2_holder_uninit(ghs + 1);
  313. gfs2_glock_dq(ghs);
  314. out_parent:
  315. gfs2_holder_uninit(ghs);
  316. gfs2_glock_dq_uninit(&ri_gh);
  317. return error;
  318. }
  319. /**
  320. * gfs2_symlink - Create a symlink
  321. * @dir: The directory to create the symlink in
  322. * @dentry: The dentry to put the symlink in
  323. * @symname: The thing which the link points to
  324. *
  325. * Returns: errno
  326. */
  327. static int gfs2_symlink(struct inode *dir, struct dentry *dentry,
  328. const char *symname)
  329. {
  330. struct gfs2_inode *dip = GFS2_I(dir), *ip;
  331. struct gfs2_sbd *sdp = GFS2_SB(dir);
  332. struct gfs2_holder ghs[2];
  333. struct inode *inode;
  334. struct buffer_head *dibh;
  335. int size;
  336. int error;
  337. /* Must be stuffed with a null terminator for gfs2_follow_link() */
  338. size = strlen(symname);
  339. if (size > sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode) - 1)
  340. return -ENAMETOOLONG;
  341. gfs2_holder_init(dip->i_gl, 0, 0, ghs);
  342. inode = gfs2_createi(ghs, &dentry->d_name, S_IFLNK | S_IRWXUGO, 0);
  343. if (IS_ERR(inode)) {
  344. gfs2_holder_uninit(ghs);
  345. return PTR_ERR(inode);
  346. }
  347. ip = ghs[1].gh_gl->gl_object;
  348. ip->i_disksize = size;
  349. i_size_write(inode, size);
  350. error = gfs2_meta_inode_buffer(ip, &dibh);
  351. if (!gfs2_assert_withdraw(sdp, !error)) {
  352. gfs2_dinode_out(ip, dibh->b_data);
  353. memcpy(dibh->b_data + sizeof(struct gfs2_dinode), symname,
  354. size);
  355. brelse(dibh);
  356. }
  357. gfs2_trans_end(sdp);
  358. if (dip->i_alloc->al_rgd)
  359. gfs2_inplace_release(dip);
  360. gfs2_quota_unlock(dip);
  361. gfs2_alloc_put(dip);
  362. gfs2_glock_dq_uninit_m(2, ghs);
  363. d_instantiate(dentry, inode);
  364. mark_inode_dirty(inode);
  365. return 0;
  366. }
  367. /**
  368. * gfs2_mkdir - Make a directory
  369. * @dir: The parent directory of the new one
  370. * @dentry: The dentry of the new directory
  371. * @mode: The mode of the new directory
  372. *
  373. * Returns: errno
  374. */
  375. static int gfs2_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  376. {
  377. struct gfs2_inode *dip = GFS2_I(dir), *ip;
  378. struct gfs2_sbd *sdp = GFS2_SB(dir);
  379. struct gfs2_holder ghs[2];
  380. struct inode *inode;
  381. struct buffer_head *dibh;
  382. int error;
  383. gfs2_holder_init(dip->i_gl, 0, 0, ghs);
  384. inode = gfs2_createi(ghs, &dentry->d_name, S_IFDIR | mode, 0);
  385. if (IS_ERR(inode)) {
  386. gfs2_holder_uninit(ghs);
  387. return PTR_ERR(inode);
  388. }
  389. ip = ghs[1].gh_gl->gl_object;
  390. ip->i_inode.i_nlink = 2;
  391. ip->i_disksize = sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode);
  392. ip->i_diskflags |= GFS2_DIF_JDATA;
  393. ip->i_entries = 2;
  394. error = gfs2_meta_inode_buffer(ip, &dibh);
  395. if (!gfs2_assert_withdraw(sdp, !error)) {
  396. struct gfs2_dinode *di = (struct gfs2_dinode *)dibh->b_data;
  397. struct gfs2_dirent *dent = (struct gfs2_dirent *)(di+1);
  398. struct qstr str;
  399. gfs2_str2qstr(&str, ".");
  400. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  401. gfs2_qstr2dirent(&str, GFS2_DIRENT_SIZE(str.len), dent);
  402. dent->de_inum = di->di_num; /* already GFS2 endian */
  403. dent->de_type = cpu_to_be16(DT_DIR);
  404. di->di_entries = cpu_to_be32(1);
  405. gfs2_str2qstr(&str, "..");
  406. dent = (struct gfs2_dirent *)((char*)dent + GFS2_DIRENT_SIZE(1));
  407. gfs2_qstr2dirent(&str, dibh->b_size - GFS2_DIRENT_SIZE(1) - sizeof(struct gfs2_dinode), dent);
  408. gfs2_inum_out(dip, dent);
  409. dent->de_type = cpu_to_be16(DT_DIR);
  410. gfs2_dinode_out(ip, di);
  411. brelse(dibh);
  412. }
  413. error = gfs2_change_nlink(dip, +1);
  414. gfs2_assert_withdraw(sdp, !error); /* dip already pinned */
  415. gfs2_trans_end(sdp);
  416. if (dip->i_alloc->al_rgd)
  417. gfs2_inplace_release(dip);
  418. gfs2_quota_unlock(dip);
  419. gfs2_alloc_put(dip);
  420. gfs2_glock_dq_uninit_m(2, ghs);
  421. d_instantiate(dentry, inode);
  422. mark_inode_dirty(inode);
  423. return 0;
  424. }
  425. /**
  426. * gfs2_rmdiri - Remove a directory
  427. * @dip: The parent directory of the directory to be removed
  428. * @name: The name of the directory to be removed
  429. * @ip: The GFS2 inode of the directory to be removed
  430. *
  431. * Assumes Glocks on dip and ip are held
  432. *
  433. * Returns: errno
  434. */
  435. static int gfs2_rmdiri(struct gfs2_inode *dip, const struct qstr *name,
  436. struct gfs2_inode *ip)
  437. {
  438. struct qstr dotname;
  439. int error;
  440. if (ip->i_entries != 2) {
  441. if (gfs2_consist_inode(ip))
  442. gfs2_dinode_print(ip);
  443. return -EIO;
  444. }
  445. error = gfs2_dir_del(dip, name);
  446. if (error)
  447. return error;
  448. error = gfs2_change_nlink(dip, -1);
  449. if (error)
  450. return error;
  451. gfs2_str2qstr(&dotname, ".");
  452. error = gfs2_dir_del(ip, &dotname);
  453. if (error)
  454. return error;
  455. gfs2_str2qstr(&dotname, "..");
  456. error = gfs2_dir_del(ip, &dotname);
  457. if (error)
  458. return error;
  459. /* It looks odd, but it really should be done twice */
  460. error = gfs2_change_nlink(ip, -1);
  461. if (error)
  462. return error;
  463. error = gfs2_change_nlink(ip, -1);
  464. if (error)
  465. return error;
  466. return error;
  467. }
  468. /**
  469. * gfs2_rmdir - Remove a directory
  470. * @dir: The parent directory of the directory to be removed
  471. * @dentry: The dentry of the directory to remove
  472. *
  473. * Remove a directory. Call gfs2_rmdiri()
  474. *
  475. * Returns: errno
  476. */
  477. static int gfs2_rmdir(struct inode *dir, struct dentry *dentry)
  478. {
  479. struct gfs2_inode *dip = GFS2_I(dir);
  480. struct gfs2_sbd *sdp = GFS2_SB(dir);
  481. struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
  482. struct gfs2_holder ghs[3];
  483. struct gfs2_rgrpd *rgd;
  484. struct gfs2_holder ri_gh;
  485. int error;
  486. error = gfs2_rindex_hold(sdp, &ri_gh);
  487. if (error)
  488. return error;
  489. gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
  490. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
  491. rgd = gfs2_blk2rgrpd(sdp, ip->i_no_addr);
  492. gfs2_holder_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + 2);
  493. error = gfs2_glock_nq(ghs); /* parent */
  494. if (error)
  495. goto out_parent;
  496. error = gfs2_glock_nq(ghs + 1); /* child */
  497. if (error)
  498. goto out_child;
  499. error = gfs2_glock_nq(ghs + 2); /* rgrp */
  500. if (error)
  501. goto out_rgrp;
  502. error = gfs2_unlink_ok(dip, &dentry->d_name, ip);
  503. if (error)
  504. goto out_gunlock;
  505. if (ip->i_entries < 2) {
  506. if (gfs2_consist_inode(ip))
  507. gfs2_dinode_print(ip);
  508. error = -EIO;
  509. goto out_gunlock;
  510. }
  511. if (ip->i_entries > 2) {
  512. error = -ENOTEMPTY;
  513. goto out_gunlock;
  514. }
  515. error = gfs2_trans_begin(sdp, 2 * RES_DINODE + 3 * RES_LEAF + RES_RG_BIT, 0);
  516. if (error)
  517. goto out_gunlock;
  518. error = gfs2_rmdiri(dip, &dentry->d_name, ip);
  519. gfs2_trans_end(sdp);
  520. out_gunlock:
  521. gfs2_glock_dq(ghs + 2);
  522. out_rgrp:
  523. gfs2_holder_uninit(ghs + 2);
  524. gfs2_glock_dq(ghs + 1);
  525. out_child:
  526. gfs2_holder_uninit(ghs + 1);
  527. gfs2_glock_dq(ghs);
  528. out_parent:
  529. gfs2_holder_uninit(ghs);
  530. gfs2_glock_dq_uninit(&ri_gh);
  531. return error;
  532. }
  533. /**
  534. * gfs2_mknod - Make a special file
  535. * @dir: The directory in which the special file will reside
  536. * @dentry: The dentry of the special file
  537. * @mode: The mode of the special file
  538. * @rdev: The device specification of the special file
  539. *
  540. */
  541. static int gfs2_mknod(struct inode *dir, struct dentry *dentry, int mode,
  542. dev_t dev)
  543. {
  544. struct gfs2_inode *dip = GFS2_I(dir);
  545. struct gfs2_sbd *sdp = GFS2_SB(dir);
  546. struct gfs2_holder ghs[2];
  547. struct inode *inode;
  548. gfs2_holder_init(dip->i_gl, 0, 0, ghs);
  549. inode = gfs2_createi(ghs, &dentry->d_name, mode, dev);
  550. if (IS_ERR(inode)) {
  551. gfs2_holder_uninit(ghs);
  552. return PTR_ERR(inode);
  553. }
  554. gfs2_trans_end(sdp);
  555. if (dip->i_alloc->al_rgd)
  556. gfs2_inplace_release(dip);
  557. gfs2_quota_unlock(dip);
  558. gfs2_alloc_put(dip);
  559. gfs2_glock_dq_uninit_m(2, ghs);
  560. d_instantiate(dentry, inode);
  561. mark_inode_dirty(inode);
  562. return 0;
  563. }
  564. /*
  565. * gfs2_ok_to_move - check if it's ok to move a directory to another directory
  566. * @this: move this
  567. * @to: to here
  568. *
  569. * Follow @to back to the root and make sure we don't encounter @this
  570. * Assumes we already hold the rename lock.
  571. *
  572. * Returns: errno
  573. */
  574. static int gfs2_ok_to_move(struct gfs2_inode *this, struct gfs2_inode *to)
  575. {
  576. struct inode *dir = &to->i_inode;
  577. struct super_block *sb = dir->i_sb;
  578. struct inode *tmp;
  579. struct qstr dotdot;
  580. int error = 0;
  581. gfs2_str2qstr(&dotdot, "..");
  582. igrab(dir);
  583. for (;;) {
  584. if (dir == &this->i_inode) {
  585. error = -EINVAL;
  586. break;
  587. }
  588. if (dir == sb->s_root->d_inode) {
  589. error = 0;
  590. break;
  591. }
  592. tmp = gfs2_lookupi(dir, &dotdot, 1);
  593. if (IS_ERR(tmp)) {
  594. error = PTR_ERR(tmp);
  595. break;
  596. }
  597. iput(dir);
  598. dir = tmp;
  599. }
  600. iput(dir);
  601. return error;
  602. }
  603. /**
  604. * gfs2_rename - Rename a file
  605. * @odir: Parent directory of old file name
  606. * @odentry: The old dentry of the file
  607. * @ndir: Parent directory of new file name
  608. * @ndentry: The new dentry of the file
  609. *
  610. * Returns: errno
  611. */
  612. static int gfs2_rename(struct inode *odir, struct dentry *odentry,
  613. struct inode *ndir, struct dentry *ndentry)
  614. {
  615. struct gfs2_inode *odip = GFS2_I(odir);
  616. struct gfs2_inode *ndip = GFS2_I(ndir);
  617. struct gfs2_inode *ip = GFS2_I(odentry->d_inode);
  618. struct gfs2_inode *nip = NULL;
  619. struct gfs2_sbd *sdp = GFS2_SB(odir);
  620. struct gfs2_holder ghs[5], r_gh = { .gh_gl = NULL, };
  621. struct gfs2_rgrpd *nrgd;
  622. unsigned int num_gh;
  623. int dir_rename = 0;
  624. int alloc_required;
  625. unsigned int x;
  626. int error;
  627. if (ndentry->d_inode) {
  628. nip = GFS2_I(ndentry->d_inode);
  629. if (ip == nip)
  630. return 0;
  631. }
  632. if (odip != ndip) {
  633. error = gfs2_glock_nq_init(sdp->sd_rename_gl, LM_ST_EXCLUSIVE,
  634. 0, &r_gh);
  635. if (error)
  636. goto out;
  637. if (S_ISDIR(ip->i_inode.i_mode)) {
  638. dir_rename = 1;
  639. /* don't move a dirctory into it's subdir */
  640. error = gfs2_ok_to_move(ip, ndip);
  641. if (error)
  642. goto out_gunlock_r;
  643. }
  644. }
  645. num_gh = 1;
  646. gfs2_holder_init(odip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
  647. if (odip != ndip) {
  648. gfs2_holder_init(ndip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
  649. num_gh++;
  650. }
  651. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
  652. num_gh++;
  653. if (nip) {
  654. gfs2_holder_init(nip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
  655. num_gh++;
  656. /* grab the resource lock for unlink flag twiddling
  657. * this is the case of the target file already existing
  658. * so we unlink before doing the rename
  659. */
  660. nrgd = gfs2_blk2rgrpd(sdp, nip->i_no_addr);
  661. if (nrgd)
  662. gfs2_holder_init(nrgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh++);
  663. }
  664. for (x = 0; x < num_gh; x++) {
  665. error = gfs2_glock_nq(ghs + x);
  666. if (error)
  667. goto out_gunlock;
  668. }
  669. /* Check out the old directory */
  670. error = gfs2_unlink_ok(odip, &odentry->d_name, ip);
  671. if (error)
  672. goto out_gunlock;
  673. /* Check out the new directory */
  674. if (nip) {
  675. error = gfs2_unlink_ok(ndip, &ndentry->d_name, nip);
  676. if (error)
  677. goto out_gunlock;
  678. if (S_ISDIR(nip->i_inode.i_mode)) {
  679. if (nip->i_entries < 2) {
  680. if (gfs2_consist_inode(nip))
  681. gfs2_dinode_print(nip);
  682. error = -EIO;
  683. goto out_gunlock;
  684. }
  685. if (nip->i_entries > 2) {
  686. error = -ENOTEMPTY;
  687. goto out_gunlock;
  688. }
  689. }
  690. } else {
  691. error = gfs2_permission(ndir, MAY_WRITE | MAY_EXEC);
  692. if (error)
  693. goto out_gunlock;
  694. error = gfs2_dir_check(ndir, &ndentry->d_name, NULL);
  695. switch (error) {
  696. case -ENOENT:
  697. error = 0;
  698. break;
  699. case 0:
  700. error = -EEXIST;
  701. default:
  702. goto out_gunlock;
  703. };
  704. if (odip != ndip) {
  705. if (!ndip->i_inode.i_nlink) {
  706. error = -EINVAL;
  707. goto out_gunlock;
  708. }
  709. if (ndip->i_entries == (u32)-1) {
  710. error = -EFBIG;
  711. goto out_gunlock;
  712. }
  713. if (S_ISDIR(ip->i_inode.i_mode) &&
  714. ndip->i_inode.i_nlink == (u32)-1) {
  715. error = -EMLINK;
  716. goto out_gunlock;
  717. }
  718. }
  719. }
  720. /* Check out the dir to be renamed */
  721. if (dir_rename) {
  722. error = gfs2_permission(odentry->d_inode, MAY_WRITE);
  723. if (error)
  724. goto out_gunlock;
  725. }
  726. alloc_required = error = gfs2_diradd_alloc_required(ndir, &ndentry->d_name);
  727. if (error < 0)
  728. goto out_gunlock;
  729. error = 0;
  730. if (alloc_required) {
  731. struct gfs2_alloc *al = gfs2_alloc_get(ndip);
  732. if (!al) {
  733. error = -ENOMEM;
  734. goto out_gunlock;
  735. }
  736. error = gfs2_quota_lock_check(ndip);
  737. if (error)
  738. goto out_alloc;
  739. al->al_requested = sdp->sd_max_dirres;
  740. error = gfs2_inplace_reserve(ndip);
  741. if (error)
  742. goto out_gunlock_q;
  743. error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
  744. al->al_rgd->rd_length +
  745. 4 * RES_DINODE + 4 * RES_LEAF +
  746. RES_STATFS + RES_QUOTA + 4, 0);
  747. if (error)
  748. goto out_ipreserv;
  749. } else {
  750. error = gfs2_trans_begin(sdp, 4 * RES_DINODE +
  751. 5 * RES_LEAF + 4, 0);
  752. if (error)
  753. goto out_gunlock;
  754. }
  755. /* Remove the target file, if it exists */
  756. if (nip) {
  757. if (S_ISDIR(nip->i_inode.i_mode))
  758. error = gfs2_rmdiri(ndip, &ndentry->d_name, nip);
  759. else {
  760. error = gfs2_dir_del(ndip, &ndentry->d_name);
  761. if (error)
  762. goto out_end_trans;
  763. error = gfs2_change_nlink(nip, -1);
  764. }
  765. if (error)
  766. goto out_end_trans;
  767. }
  768. if (dir_rename) {
  769. struct qstr name;
  770. gfs2_str2qstr(&name, "..");
  771. error = gfs2_change_nlink(ndip, +1);
  772. if (error)
  773. goto out_end_trans;
  774. error = gfs2_change_nlink(odip, -1);
  775. if (error)
  776. goto out_end_trans;
  777. error = gfs2_dir_mvino(ip, &name, ndip, DT_DIR);
  778. if (error)
  779. goto out_end_trans;
  780. } else {
  781. struct buffer_head *dibh;
  782. error = gfs2_meta_inode_buffer(ip, &dibh);
  783. if (error)
  784. goto out_end_trans;
  785. ip->i_inode.i_ctime = CURRENT_TIME;
  786. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  787. gfs2_dinode_out(ip, dibh->b_data);
  788. brelse(dibh);
  789. }
  790. error = gfs2_dir_del(odip, &odentry->d_name);
  791. if (error)
  792. goto out_end_trans;
  793. error = gfs2_dir_add(ndir, &ndentry->d_name, ip, IF2DT(ip->i_inode.i_mode));
  794. if (error)
  795. goto out_end_trans;
  796. out_end_trans:
  797. gfs2_trans_end(sdp);
  798. out_ipreserv:
  799. if (alloc_required)
  800. gfs2_inplace_release(ndip);
  801. out_gunlock_q:
  802. if (alloc_required)
  803. gfs2_quota_unlock(ndip);
  804. out_alloc:
  805. if (alloc_required)
  806. gfs2_alloc_put(ndip);
  807. out_gunlock:
  808. while (x--) {
  809. gfs2_glock_dq(ghs + x);
  810. gfs2_holder_uninit(ghs + x);
  811. }
  812. out_gunlock_r:
  813. if (r_gh.gh_gl)
  814. gfs2_glock_dq_uninit(&r_gh);
  815. out:
  816. return error;
  817. }
  818. /**
  819. * gfs2_readlinki - return the contents of a symlink
  820. * @ip: the symlink's inode
  821. * @buf: a pointer to the buffer to be filled
  822. * @len: a pointer to the length of @buf
  823. *
  824. * If @buf is too small, a piece of memory is kmalloc()ed and needs
  825. * to be freed by the caller.
  826. *
  827. * Returns: errno
  828. */
  829. static int gfs2_readlinki(struct gfs2_inode *ip, char **buf, unsigned int *len)
  830. {
  831. struct gfs2_holder i_gh;
  832. struct buffer_head *dibh;
  833. unsigned int x;
  834. int error;
  835. gfs2_holder_init(ip->i_gl, LM_ST_SHARED, 0, &i_gh);
  836. error = gfs2_glock_nq(&i_gh);
  837. if (error) {
  838. gfs2_holder_uninit(&i_gh);
  839. return error;
  840. }
  841. if (!ip->i_disksize) {
  842. gfs2_consist_inode(ip);
  843. error = -EIO;
  844. goto out;
  845. }
  846. error = gfs2_meta_inode_buffer(ip, &dibh);
  847. if (error)
  848. goto out;
  849. x = ip->i_disksize + 1;
  850. if (x > *len) {
  851. *buf = kmalloc(x, GFP_NOFS);
  852. if (!*buf) {
  853. error = -ENOMEM;
  854. goto out_brelse;
  855. }
  856. }
  857. memcpy(*buf, dibh->b_data + sizeof(struct gfs2_dinode), x);
  858. *len = x;
  859. out_brelse:
  860. brelse(dibh);
  861. out:
  862. gfs2_glock_dq_uninit(&i_gh);
  863. return error;
  864. }
  865. /**
  866. * gfs2_readlink - Read the value of a symlink
  867. * @dentry: the symlink
  868. * @buf: the buffer to read the symlink data into
  869. * @size: the size of the buffer
  870. *
  871. * Returns: errno
  872. */
  873. static int gfs2_readlink(struct dentry *dentry, char __user *user_buf,
  874. int user_size)
  875. {
  876. struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
  877. char array[GFS2_FAST_NAME_SIZE], *buf = array;
  878. unsigned int len = GFS2_FAST_NAME_SIZE;
  879. int error;
  880. error = gfs2_readlinki(ip, &buf, &len);
  881. if (error)
  882. return error;
  883. if (user_size > len - 1)
  884. user_size = len - 1;
  885. if (copy_to_user(user_buf, buf, user_size))
  886. error = -EFAULT;
  887. else
  888. error = user_size;
  889. if (buf != array)
  890. kfree(buf);
  891. return error;
  892. }
  893. /**
  894. * gfs2_follow_link - Follow a symbolic link
  895. * @dentry: The dentry of the link
  896. * @nd: Data that we pass to vfs_follow_link()
  897. *
  898. * This can handle symlinks of any size. It is optimised for symlinks
  899. * under GFS2_FAST_NAME_SIZE.
  900. *
  901. * Returns: 0 on success or error code
  902. */
  903. static void *gfs2_follow_link(struct dentry *dentry, struct nameidata *nd)
  904. {
  905. struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
  906. char array[GFS2_FAST_NAME_SIZE], *buf = array;
  907. unsigned int len = GFS2_FAST_NAME_SIZE;
  908. int error;
  909. error = gfs2_readlinki(ip, &buf, &len);
  910. if (!error) {
  911. error = vfs_follow_link(nd, buf);
  912. if (buf != array)
  913. kfree(buf);
  914. }
  915. return ERR_PTR(error);
  916. }
  917. /**
  918. * gfs2_permission -
  919. * @inode:
  920. * @mask:
  921. * @nd: passed from Linux VFS, ignored by us
  922. *
  923. * This may be called from the VFS directly, or from within GFS2 with the
  924. * inode locked, so we look to see if the glock is already locked and only
  925. * lock the glock if its not already been done.
  926. *
  927. * Returns: errno
  928. */
  929. int gfs2_permission(struct inode *inode, int mask)
  930. {
  931. struct gfs2_inode *ip = GFS2_I(inode);
  932. struct gfs2_holder i_gh;
  933. int error;
  934. int unlock = 0;
  935. if (gfs2_glock_is_locked_by_me(ip->i_gl) == NULL) {
  936. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh);
  937. if (error)
  938. return error;
  939. unlock = 1;
  940. }
  941. if ((mask & MAY_WRITE) && IS_IMMUTABLE(inode))
  942. error = -EACCES;
  943. else
  944. error = generic_permission(inode, mask, gfs2_check_acl);
  945. if (unlock)
  946. gfs2_glock_dq_uninit(&i_gh);
  947. return error;
  948. }
  949. static int setattr_size(struct inode *inode, struct iattr *attr)
  950. {
  951. struct gfs2_inode *ip = GFS2_I(inode);
  952. struct gfs2_sbd *sdp = GFS2_SB(inode);
  953. int error;
  954. if (attr->ia_size != ip->i_disksize) {
  955. error = gfs2_trans_begin(sdp, 0, sdp->sd_jdesc->jd_blocks);
  956. if (error)
  957. return error;
  958. error = vmtruncate(inode, attr->ia_size);
  959. gfs2_trans_end(sdp);
  960. if (error)
  961. return error;
  962. }
  963. error = gfs2_truncatei(ip, attr->ia_size);
  964. if (error && (inode->i_size != ip->i_disksize))
  965. i_size_write(inode, ip->i_disksize);
  966. return error;
  967. }
  968. static int setattr_chown(struct inode *inode, struct iattr *attr)
  969. {
  970. struct gfs2_inode *ip = GFS2_I(inode);
  971. struct gfs2_sbd *sdp = GFS2_SB(inode);
  972. struct buffer_head *dibh;
  973. u32 ouid, ogid, nuid, ngid;
  974. int error;
  975. ouid = inode->i_uid;
  976. ogid = inode->i_gid;
  977. nuid = attr->ia_uid;
  978. ngid = attr->ia_gid;
  979. if (!(attr->ia_valid & ATTR_UID) || ouid == nuid)
  980. ouid = nuid = NO_QUOTA_CHANGE;
  981. if (!(attr->ia_valid & ATTR_GID) || ogid == ngid)
  982. ogid = ngid = NO_QUOTA_CHANGE;
  983. if (!gfs2_alloc_get(ip))
  984. return -ENOMEM;
  985. error = gfs2_quota_lock(ip, nuid, ngid);
  986. if (error)
  987. goto out_alloc;
  988. if (ouid != NO_QUOTA_CHANGE || ogid != NO_QUOTA_CHANGE) {
  989. error = gfs2_quota_check(ip, nuid, ngid);
  990. if (error)
  991. goto out_gunlock_q;
  992. }
  993. error = gfs2_trans_begin(sdp, RES_DINODE + 2 * RES_QUOTA, 0);
  994. if (error)
  995. goto out_gunlock_q;
  996. error = gfs2_meta_inode_buffer(ip, &dibh);
  997. if (error)
  998. goto out_end_trans;
  999. error = inode_setattr(inode, attr);
  1000. gfs2_assert_warn(sdp, !error);
  1001. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  1002. gfs2_dinode_out(ip, dibh->b_data);
  1003. brelse(dibh);
  1004. if (ouid != NO_QUOTA_CHANGE || ogid != NO_QUOTA_CHANGE) {
  1005. u64 blocks = gfs2_get_inode_blocks(&ip->i_inode);
  1006. gfs2_quota_change(ip, -blocks, ouid, ogid);
  1007. gfs2_quota_change(ip, blocks, nuid, ngid);
  1008. }
  1009. out_end_trans:
  1010. gfs2_trans_end(sdp);
  1011. out_gunlock_q:
  1012. gfs2_quota_unlock(ip);
  1013. out_alloc:
  1014. gfs2_alloc_put(ip);
  1015. return error;
  1016. }
  1017. /**
  1018. * gfs2_setattr - Change attributes on an inode
  1019. * @dentry: The dentry which is changing
  1020. * @attr: The structure describing the change
  1021. *
  1022. * The VFS layer wants to change one or more of an inodes attributes. Write
  1023. * that change out to disk.
  1024. *
  1025. * Returns: errno
  1026. */
  1027. static int gfs2_setattr(struct dentry *dentry, struct iattr *attr)
  1028. {
  1029. struct inode *inode = dentry->d_inode;
  1030. struct gfs2_inode *ip = GFS2_I(inode);
  1031. struct gfs2_holder i_gh;
  1032. int error;
  1033. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
  1034. if (error)
  1035. return error;
  1036. error = -EPERM;
  1037. if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
  1038. goto out;
  1039. error = inode_change_ok(inode, attr);
  1040. if (error)
  1041. goto out;
  1042. if (attr->ia_valid & ATTR_SIZE)
  1043. error = setattr_size(inode, attr);
  1044. else if (attr->ia_valid & (ATTR_UID | ATTR_GID))
  1045. error = setattr_chown(inode, attr);
  1046. else if ((attr->ia_valid & ATTR_MODE) && IS_POSIXACL(inode))
  1047. error = gfs2_acl_chmod(ip, attr);
  1048. else
  1049. error = gfs2_setattr_simple(ip, attr);
  1050. out:
  1051. gfs2_glock_dq_uninit(&i_gh);
  1052. if (!error)
  1053. mark_inode_dirty(inode);
  1054. return error;
  1055. }
  1056. /**
  1057. * gfs2_getattr - Read out an inode's attributes
  1058. * @mnt: The vfsmount the inode is being accessed from
  1059. * @dentry: The dentry to stat
  1060. * @stat: The inode's stats
  1061. *
  1062. * This may be called from the VFS directly, or from within GFS2 with the
  1063. * inode locked, so we look to see if the glock is already locked and only
  1064. * lock the glock if its not already been done. Note that its the NFS
  1065. * readdirplus operation which causes this to be called (from filldir)
  1066. * with the glock already held.
  1067. *
  1068. * Returns: errno
  1069. */
  1070. static int gfs2_getattr(struct vfsmount *mnt, struct dentry *dentry,
  1071. struct kstat *stat)
  1072. {
  1073. struct inode *inode = dentry->d_inode;
  1074. struct gfs2_inode *ip = GFS2_I(inode);
  1075. struct gfs2_holder gh;
  1076. int error;
  1077. int unlock = 0;
  1078. if (gfs2_glock_is_locked_by_me(ip->i_gl) == NULL) {
  1079. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
  1080. if (error)
  1081. return error;
  1082. unlock = 1;
  1083. }
  1084. generic_fillattr(inode, stat);
  1085. if (unlock)
  1086. gfs2_glock_dq_uninit(&gh);
  1087. return 0;
  1088. }
  1089. static int gfs2_setxattr(struct dentry *dentry, const char *name,
  1090. const void *data, size_t size, int flags)
  1091. {
  1092. struct inode *inode = dentry->d_inode;
  1093. struct gfs2_inode *ip = GFS2_I(inode);
  1094. struct gfs2_holder gh;
  1095. int ret;
  1096. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
  1097. ret = gfs2_glock_nq(&gh);
  1098. if (ret == 0) {
  1099. ret = generic_setxattr(dentry, name, data, size, flags);
  1100. gfs2_glock_dq(&gh);
  1101. }
  1102. gfs2_holder_uninit(&gh);
  1103. return ret;
  1104. }
  1105. static ssize_t gfs2_getxattr(struct dentry *dentry, const char *name,
  1106. void *data, size_t size)
  1107. {
  1108. struct inode *inode = dentry->d_inode;
  1109. struct gfs2_inode *ip = GFS2_I(inode);
  1110. struct gfs2_holder gh;
  1111. int ret;
  1112. gfs2_holder_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
  1113. ret = gfs2_glock_nq(&gh);
  1114. if (ret == 0) {
  1115. ret = generic_getxattr(dentry, name, data, size);
  1116. gfs2_glock_dq(&gh);
  1117. }
  1118. gfs2_holder_uninit(&gh);
  1119. return ret;
  1120. }
  1121. static int gfs2_removexattr(struct dentry *dentry, const char *name)
  1122. {
  1123. struct inode *inode = dentry->d_inode;
  1124. struct gfs2_inode *ip = GFS2_I(inode);
  1125. struct gfs2_holder gh;
  1126. int ret;
  1127. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
  1128. ret = gfs2_glock_nq(&gh);
  1129. if (ret == 0) {
  1130. ret = generic_removexattr(dentry, name);
  1131. gfs2_glock_dq(&gh);
  1132. }
  1133. gfs2_holder_uninit(&gh);
  1134. return ret;
  1135. }
  1136. static int gfs2_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
  1137. u64 start, u64 len)
  1138. {
  1139. struct gfs2_inode *ip = GFS2_I(inode);
  1140. struct gfs2_holder gh;
  1141. int ret;
  1142. ret = fiemap_check_flags(fieinfo, FIEMAP_FLAG_SYNC);
  1143. if (ret)
  1144. return ret;
  1145. mutex_lock(&inode->i_mutex);
  1146. ret = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &gh);
  1147. if (ret)
  1148. goto out;
  1149. if (gfs2_is_stuffed(ip)) {
  1150. u64 phys = ip->i_no_addr << inode->i_blkbits;
  1151. u64 size = i_size_read(inode);
  1152. u32 flags = FIEMAP_EXTENT_LAST|FIEMAP_EXTENT_NOT_ALIGNED|
  1153. FIEMAP_EXTENT_DATA_INLINE;
  1154. phys += sizeof(struct gfs2_dinode);
  1155. phys += start;
  1156. if (start + len > size)
  1157. len = size - start;
  1158. if (start < size)
  1159. ret = fiemap_fill_next_extent(fieinfo, start, phys,
  1160. len, flags);
  1161. if (ret == 1)
  1162. ret = 0;
  1163. } else {
  1164. ret = __generic_block_fiemap(inode, fieinfo, start, len,
  1165. gfs2_block_map);
  1166. }
  1167. gfs2_glock_dq_uninit(&gh);
  1168. out:
  1169. mutex_unlock(&inode->i_mutex);
  1170. return ret;
  1171. }
  1172. const struct inode_operations gfs2_file_iops = {
  1173. .permission = gfs2_permission,
  1174. .setattr = gfs2_setattr,
  1175. .getattr = gfs2_getattr,
  1176. .setxattr = gfs2_setxattr,
  1177. .getxattr = gfs2_getxattr,
  1178. .listxattr = gfs2_listxattr,
  1179. .removexattr = gfs2_removexattr,
  1180. .fiemap = gfs2_fiemap,
  1181. };
  1182. const struct inode_operations gfs2_dir_iops = {
  1183. .create = gfs2_create,
  1184. .lookup = gfs2_lookup,
  1185. .link = gfs2_link,
  1186. .unlink = gfs2_unlink,
  1187. .symlink = gfs2_symlink,
  1188. .mkdir = gfs2_mkdir,
  1189. .rmdir = gfs2_rmdir,
  1190. .mknod = gfs2_mknod,
  1191. .rename = gfs2_rename,
  1192. .permission = gfs2_permission,
  1193. .setattr = gfs2_setattr,
  1194. .getattr = gfs2_getattr,
  1195. .setxattr = gfs2_setxattr,
  1196. .getxattr = gfs2_getxattr,
  1197. .listxattr = gfs2_listxattr,
  1198. .removexattr = gfs2_removexattr,
  1199. .fiemap = gfs2_fiemap,
  1200. };
  1201. const struct inode_operations gfs2_symlink_iops = {
  1202. .readlink = gfs2_readlink,
  1203. .follow_link = gfs2_follow_link,
  1204. .permission = gfs2_permission,
  1205. .setattr = gfs2_setattr,
  1206. .getattr = gfs2_getattr,
  1207. .setxattr = gfs2_setxattr,
  1208. .getxattr = gfs2_getxattr,
  1209. .listxattr = gfs2_listxattr,
  1210. .removexattr = gfs2_removexattr,
  1211. .fiemap = gfs2_fiemap,
  1212. };