ops_inode.c 27 KB

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