ops_inode.c 31 KB

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