ops_inode.c 26 KB

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