ops_inode.c 27 KB

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