ops_inode.c 29 KB

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