ops_inode.c 26 KB

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