ops_inode.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120
  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_payload_format = GFS2_FORMAT_DE;
  332. ip->i_di.di_entries = 2;
  333. error = gfs2_meta_inode_buffer(ip, &dibh);
  334. if (!gfs2_assert_withdraw(sdp, !error)) {
  335. struct gfs2_dinode *di = (struct gfs2_dinode *)dibh->b_data;
  336. struct gfs2_dirent *dent = (struct gfs2_dirent *)(di+1);
  337. struct qstr str;
  338. gfs2_str2qstr(&str, ".");
  339. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  340. gfs2_qstr2dirent(&str, GFS2_DIRENT_SIZE(str.len), dent);
  341. dent->de_inum = di->di_num; /* already GFS2 endian */
  342. dent->de_type = cpu_to_be16(DT_DIR);
  343. di->di_entries = cpu_to_be32(1);
  344. gfs2_str2qstr(&str, "..");
  345. dent = (struct gfs2_dirent *)((char*)dent + GFS2_DIRENT_SIZE(1));
  346. gfs2_qstr2dirent(&str, dibh->b_size - GFS2_DIRENT_SIZE(1) - sizeof(struct gfs2_dinode), dent);
  347. gfs2_inum_out(&dip->i_num, &dent->de_inum);
  348. dent->de_type = cpu_to_be16(DT_DIR);
  349. gfs2_dinode_out(ip, di);
  350. brelse(dibh);
  351. }
  352. error = gfs2_change_nlink(dip, +1);
  353. gfs2_assert_withdraw(sdp, !error); /* dip already pinned */
  354. gfs2_trans_end(sdp);
  355. if (dip->i_alloc.al_rgd)
  356. gfs2_inplace_release(dip);
  357. gfs2_quota_unlock(dip);
  358. gfs2_alloc_put(dip);
  359. gfs2_glock_dq_uninit_m(2, ghs);
  360. d_instantiate(dentry, inode);
  361. mark_inode_dirty(inode);
  362. return 0;
  363. }
  364. /**
  365. * gfs2_rmdir - Remove a directory
  366. * @dir: The parent directory of the directory to be removed
  367. * @dentry: The dentry of the directory to remove
  368. *
  369. * Remove a directory. Call gfs2_rmdiri()
  370. *
  371. * Returns: errno
  372. */
  373. static int gfs2_rmdir(struct inode *dir, struct dentry *dentry)
  374. {
  375. struct gfs2_inode *dip = GFS2_I(dir);
  376. struct gfs2_sbd *sdp = GFS2_SB(dir);
  377. struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
  378. struct gfs2_holder ghs[2];
  379. int error;
  380. gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
  381. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
  382. error = gfs2_glock_nq_m(2, ghs);
  383. if (error)
  384. goto out;
  385. error = gfs2_unlink_ok(dip, &dentry->d_name, ip);
  386. if (error)
  387. goto out_gunlock;
  388. if (ip->i_di.di_entries < 2) {
  389. if (gfs2_consist_inode(ip))
  390. gfs2_dinode_print(ip);
  391. error = -EIO;
  392. goto out_gunlock;
  393. }
  394. if (ip->i_di.di_entries > 2) {
  395. error = -ENOTEMPTY;
  396. goto out_gunlock;
  397. }
  398. error = gfs2_trans_begin(sdp, 2 * RES_DINODE + 3 * RES_LEAF + RES_RG_BIT, 0);
  399. if (error)
  400. goto out_gunlock;
  401. error = gfs2_rmdiri(dip, &dentry->d_name, ip);
  402. gfs2_trans_end(sdp);
  403. out_gunlock:
  404. gfs2_glock_dq_m(2, ghs);
  405. out:
  406. gfs2_holder_uninit(ghs);
  407. gfs2_holder_uninit(ghs + 1);
  408. return error;
  409. }
  410. /**
  411. * gfs2_mknod - Make a special file
  412. * @dir: The directory in which the special file will reside
  413. * @dentry: The dentry of the special file
  414. * @mode: The mode of the special file
  415. * @rdev: The device specification of the special file
  416. *
  417. */
  418. static int gfs2_mknod(struct inode *dir, struct dentry *dentry, int mode,
  419. dev_t dev)
  420. {
  421. struct gfs2_inode *dip = GFS2_I(dir);
  422. struct gfs2_sbd *sdp = GFS2_SB(dir);
  423. struct gfs2_holder ghs[2];
  424. struct inode *inode;
  425. gfs2_holder_init(dip->i_gl, 0, 0, ghs);
  426. inode = gfs2_createi(ghs, &dentry->d_name, mode, dev);
  427. if (IS_ERR(inode)) {
  428. gfs2_holder_uninit(ghs);
  429. return PTR_ERR(inode);
  430. }
  431. gfs2_trans_end(sdp);
  432. if (dip->i_alloc.al_rgd)
  433. gfs2_inplace_release(dip);
  434. gfs2_quota_unlock(dip);
  435. gfs2_alloc_put(dip);
  436. gfs2_glock_dq_uninit_m(2, ghs);
  437. d_instantiate(dentry, inode);
  438. mark_inode_dirty(inode);
  439. return 0;
  440. }
  441. /**
  442. * gfs2_rename - Rename a file
  443. * @odir: Parent directory of old file name
  444. * @odentry: The old dentry of the file
  445. * @ndir: Parent directory of new file name
  446. * @ndentry: The new dentry of the file
  447. *
  448. * Returns: errno
  449. */
  450. static int gfs2_rename(struct inode *odir, struct dentry *odentry,
  451. struct inode *ndir, struct dentry *ndentry)
  452. {
  453. struct gfs2_inode *odip = GFS2_I(odir);
  454. struct gfs2_inode *ndip = GFS2_I(ndir);
  455. struct gfs2_inode *ip = GFS2_I(odentry->d_inode);
  456. struct gfs2_inode *nip = NULL;
  457. struct gfs2_sbd *sdp = GFS2_SB(odir);
  458. struct gfs2_holder ghs[4], r_gh;
  459. unsigned int num_gh;
  460. int dir_rename = 0;
  461. int alloc_required;
  462. unsigned int x;
  463. int error;
  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, 0);
  572. if (error)
  573. goto out_ipreserv;
  574. } else {
  575. error = gfs2_trans_begin(sdp, 4 * RES_DINODE +
  576. 5 * RES_LEAF, 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(nip, -1);
  589. }
  590. if (error)
  591. goto out_end_trans;
  592. }
  593. if (dir_rename) {
  594. struct qstr name;
  595. gfs2_str2qstr(&name, "..");
  596. error = gfs2_change_nlink(ndip, +1);
  597. if (error)
  598. goto out_end_trans;
  599. error = gfs2_change_nlink(odip, -1);
  600. if (error)
  601. goto out_end_trans;
  602. error = gfs2_dir_mvino(ip, &name, &ndip->i_num, DT_DIR);
  603. if (error)
  604. goto out_end_trans;
  605. } else {
  606. struct buffer_head *dibh;
  607. error = gfs2_meta_inode_buffer(ip, &dibh);
  608. if (error)
  609. goto out_end_trans;
  610. ip->i_di.di_ctime = get_seconds();
  611. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  612. gfs2_dinode_out(ip, dibh->b_data);
  613. brelse(dibh);
  614. }
  615. error = gfs2_dir_del(odip, &odentry->d_name);
  616. if (error)
  617. goto out_end_trans;
  618. error = gfs2_dir_add(ndir, &ndentry->d_name, &ip->i_num,
  619. IF2DT(ip->i_inode.i_mode));
  620. if (error)
  621. goto out_end_trans;
  622. out_end_trans:
  623. gfs2_trans_end(sdp);
  624. out_ipreserv:
  625. if (alloc_required)
  626. gfs2_inplace_release(ndip);
  627. out_gunlock_q:
  628. if (alloc_required)
  629. gfs2_quota_unlock(ndip);
  630. out_alloc:
  631. if (alloc_required)
  632. gfs2_alloc_put(ndip);
  633. out_gunlock:
  634. gfs2_glock_dq_m(num_gh, ghs);
  635. out_uninit:
  636. for (x = 0; x < num_gh; x++)
  637. gfs2_holder_uninit(ghs + x);
  638. out_gunlock_r:
  639. if (dir_rename)
  640. gfs2_glock_dq_uninit(&r_gh);
  641. out:
  642. return error;
  643. }
  644. /**
  645. * gfs2_readlink - Read the value of a symlink
  646. * @dentry: the symlink
  647. * @buf: the buffer to read the symlink data into
  648. * @size: the size of the buffer
  649. *
  650. * Returns: errno
  651. */
  652. static int gfs2_readlink(struct dentry *dentry, char __user *user_buf,
  653. int user_size)
  654. {
  655. struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
  656. char array[GFS2_FAST_NAME_SIZE], *buf = array;
  657. unsigned int len = GFS2_FAST_NAME_SIZE;
  658. int error;
  659. error = gfs2_readlinki(ip, &buf, &len);
  660. if (error)
  661. return error;
  662. if (user_size > len - 1)
  663. user_size = len - 1;
  664. if (copy_to_user(user_buf, buf, user_size))
  665. error = -EFAULT;
  666. else
  667. error = user_size;
  668. if (buf != array)
  669. kfree(buf);
  670. return error;
  671. }
  672. /**
  673. * gfs2_follow_link - Follow a symbolic link
  674. * @dentry: The dentry of the link
  675. * @nd: Data that we pass to vfs_follow_link()
  676. *
  677. * This can handle symlinks of any size. It is optimised for symlinks
  678. * under GFS2_FAST_NAME_SIZE.
  679. *
  680. * Returns: 0 on success or error code
  681. */
  682. static void *gfs2_follow_link(struct dentry *dentry, struct nameidata *nd)
  683. {
  684. struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
  685. char array[GFS2_FAST_NAME_SIZE], *buf = array;
  686. unsigned int len = GFS2_FAST_NAME_SIZE;
  687. int error;
  688. error = gfs2_readlinki(ip, &buf, &len);
  689. if (!error) {
  690. error = vfs_follow_link(nd, buf);
  691. if (buf != array)
  692. kfree(buf);
  693. }
  694. return ERR_PTR(error);
  695. }
  696. /**
  697. * gfs2_permission -
  698. * @inode:
  699. * @mask:
  700. * @nd: passed from Linux VFS, ignored by us
  701. *
  702. * Returns: errno
  703. */
  704. static int gfs2_permission(struct inode *inode, int mask, struct nameidata *nd)
  705. {
  706. struct gfs2_inode *ip = GFS2_I(inode);
  707. struct gfs2_holder i_gh;
  708. int error;
  709. if (ip->i_vn == ip->i_gl->gl_vn)
  710. return generic_permission(inode, mask, gfs2_check_acl);
  711. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh);
  712. if (!error) {
  713. error = generic_permission(inode, mask, gfs2_check_acl_locked);
  714. gfs2_glock_dq_uninit(&i_gh);
  715. }
  716. return error;
  717. }
  718. static int setattr_size(struct inode *inode, struct iattr *attr)
  719. {
  720. struct gfs2_inode *ip = GFS2_I(inode);
  721. int error;
  722. if (attr->ia_size != ip->i_di.di_size) {
  723. error = vmtruncate(inode, attr->ia_size);
  724. if (error)
  725. return error;
  726. }
  727. error = gfs2_truncatei(ip, attr->ia_size);
  728. if (error)
  729. return error;
  730. return error;
  731. }
  732. static int setattr_chown(struct inode *inode, struct iattr *attr)
  733. {
  734. struct gfs2_inode *ip = GFS2_I(inode);
  735. struct gfs2_sbd *sdp = GFS2_SB(inode);
  736. struct buffer_head *dibh;
  737. u32 ouid, ogid, nuid, ngid;
  738. int error;
  739. ouid = inode->i_uid;
  740. ogid = inode->i_gid;
  741. nuid = attr->ia_uid;
  742. ngid = attr->ia_gid;
  743. if (!(attr->ia_valid & ATTR_UID) || ouid == nuid)
  744. ouid = nuid = NO_QUOTA_CHANGE;
  745. if (!(attr->ia_valid & ATTR_GID) || ogid == ngid)
  746. ogid = ngid = NO_QUOTA_CHANGE;
  747. gfs2_alloc_get(ip);
  748. error = gfs2_quota_lock(ip, nuid, ngid);
  749. if (error)
  750. goto out_alloc;
  751. if (ouid != NO_QUOTA_CHANGE || ogid != NO_QUOTA_CHANGE) {
  752. error = gfs2_quota_check(ip, nuid, ngid);
  753. if (error)
  754. goto out_gunlock_q;
  755. }
  756. error = gfs2_trans_begin(sdp, RES_DINODE + 2 * RES_QUOTA, 0);
  757. if (error)
  758. goto out_gunlock_q;
  759. error = gfs2_meta_inode_buffer(ip, &dibh);
  760. if (error)
  761. goto out_end_trans;
  762. error = inode_setattr(inode, attr);
  763. gfs2_assert_warn(sdp, !error);
  764. gfs2_inode_attr_out(ip);
  765. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  766. gfs2_dinode_out(ip, dibh->b_data);
  767. brelse(dibh);
  768. if (ouid != NO_QUOTA_CHANGE || ogid != NO_QUOTA_CHANGE) {
  769. gfs2_quota_change(ip, -ip->i_di.di_blocks, ouid, ogid);
  770. gfs2_quota_change(ip, ip->i_di.di_blocks, nuid, ngid);
  771. }
  772. out_end_trans:
  773. gfs2_trans_end(sdp);
  774. out_gunlock_q:
  775. gfs2_quota_unlock(ip);
  776. out_alloc:
  777. gfs2_alloc_put(ip);
  778. return error;
  779. }
  780. /**
  781. * gfs2_setattr - Change attributes on an inode
  782. * @dentry: The dentry which is changing
  783. * @attr: The structure describing the change
  784. *
  785. * The VFS layer wants to change one or more of an inodes attributes. Write
  786. * that change out to disk.
  787. *
  788. * Returns: errno
  789. */
  790. static int gfs2_setattr(struct dentry *dentry, struct iattr *attr)
  791. {
  792. struct inode *inode = dentry->d_inode;
  793. struct gfs2_inode *ip = GFS2_I(inode);
  794. struct gfs2_holder i_gh;
  795. int error;
  796. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
  797. if (error)
  798. return error;
  799. error = -EPERM;
  800. if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
  801. goto out;
  802. error = inode_change_ok(inode, attr);
  803. if (error)
  804. goto out;
  805. if (attr->ia_valid & ATTR_SIZE)
  806. error = setattr_size(inode, attr);
  807. else if (attr->ia_valid & (ATTR_UID | ATTR_GID))
  808. error = setattr_chown(inode, attr);
  809. else if ((attr->ia_valid & ATTR_MODE) && IS_POSIXACL(inode))
  810. error = gfs2_acl_chmod(ip, attr);
  811. else
  812. error = gfs2_setattr_simple(ip, attr);
  813. out:
  814. gfs2_glock_dq_uninit(&i_gh);
  815. if (!error)
  816. mark_inode_dirty(inode);
  817. return error;
  818. }
  819. /**
  820. * gfs2_getattr - Read out an inode's attributes
  821. * @mnt: The vfsmount the inode is being accessed from
  822. * @dentry: The dentry to stat
  823. * @stat: The inode's stats
  824. *
  825. * Returns: errno
  826. */
  827. static int gfs2_getattr(struct vfsmount *mnt, struct dentry *dentry,
  828. struct kstat *stat)
  829. {
  830. struct inode *inode = dentry->d_inode;
  831. struct gfs2_inode *ip = GFS2_I(inode);
  832. struct gfs2_holder gh;
  833. int error;
  834. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
  835. if (!error) {
  836. generic_fillattr(inode, stat);
  837. gfs2_glock_dq_uninit(&gh);
  838. }
  839. return error;
  840. }
  841. static int gfs2_setxattr(struct dentry *dentry, const char *name,
  842. const void *data, size_t size, int flags)
  843. {
  844. struct inode *inode = dentry->d_inode;
  845. struct gfs2_ea_request er;
  846. memset(&er, 0, sizeof(struct gfs2_ea_request));
  847. er.er_type = gfs2_ea_name2type(name, &er.er_name);
  848. if (er.er_type == GFS2_EATYPE_UNUSED)
  849. return -EOPNOTSUPP;
  850. er.er_data = (char *)data;
  851. er.er_name_len = strlen(er.er_name);
  852. er.er_data_len = size;
  853. er.er_flags = flags;
  854. gfs2_assert_warn(GFS2_SB(inode), !(er.er_flags & GFS2_ERF_MODE));
  855. return gfs2_ea_set(GFS2_I(inode), &er);
  856. }
  857. static ssize_t gfs2_getxattr(struct dentry *dentry, const char *name,
  858. void *data, size_t size)
  859. {
  860. struct gfs2_ea_request er;
  861. memset(&er, 0, sizeof(struct gfs2_ea_request));
  862. er.er_type = gfs2_ea_name2type(name, &er.er_name);
  863. if (er.er_type == GFS2_EATYPE_UNUSED)
  864. return -EOPNOTSUPP;
  865. er.er_data = data;
  866. er.er_name_len = strlen(er.er_name);
  867. er.er_data_len = size;
  868. return gfs2_ea_get(GFS2_I(dentry->d_inode), &er);
  869. }
  870. static ssize_t gfs2_listxattr(struct dentry *dentry, char *buffer, size_t size)
  871. {
  872. struct gfs2_ea_request er;
  873. memset(&er, 0, sizeof(struct gfs2_ea_request));
  874. er.er_data = (size) ? buffer : NULL;
  875. er.er_data_len = size;
  876. return gfs2_ea_list(GFS2_I(dentry->d_inode), &er);
  877. }
  878. static int gfs2_removexattr(struct dentry *dentry, const char *name)
  879. {
  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_name_len = strlen(er.er_name);
  886. return gfs2_ea_remove(GFS2_I(dentry->d_inode), &er);
  887. }
  888. struct inode_operations gfs2_file_iops = {
  889. .permission = gfs2_permission,
  890. .setattr = gfs2_setattr,
  891. .getattr = gfs2_getattr,
  892. .setxattr = gfs2_setxattr,
  893. .getxattr = gfs2_getxattr,
  894. .listxattr = gfs2_listxattr,
  895. .removexattr = gfs2_removexattr,
  896. };
  897. struct inode_operations gfs2_dev_iops = {
  898. .permission = gfs2_permission,
  899. .setattr = gfs2_setattr,
  900. .getattr = gfs2_getattr,
  901. .setxattr = gfs2_setxattr,
  902. .getxattr = gfs2_getxattr,
  903. .listxattr = gfs2_listxattr,
  904. .removexattr = gfs2_removexattr,
  905. };
  906. struct inode_operations gfs2_dir_iops = {
  907. .create = gfs2_create,
  908. .lookup = gfs2_lookup,
  909. .link = gfs2_link,
  910. .unlink = gfs2_unlink,
  911. .symlink = gfs2_symlink,
  912. .mkdir = gfs2_mkdir,
  913. .rmdir = gfs2_rmdir,
  914. .mknod = gfs2_mknod,
  915. .rename = gfs2_rename,
  916. .permission = gfs2_permission,
  917. .setattr = gfs2_setattr,
  918. .getattr = gfs2_getattr,
  919. .setxattr = gfs2_setxattr,
  920. .getxattr = gfs2_getxattr,
  921. .listxattr = gfs2_listxattr,
  922. .removexattr = gfs2_removexattr,
  923. };
  924. struct inode_operations gfs2_symlink_iops = {
  925. .readlink = gfs2_readlink,
  926. .follow_link = gfs2_follow_link,
  927. .permission = gfs2_permission,
  928. .setattr = gfs2_setattr,
  929. .getattr = gfs2_getattr,
  930. .setxattr = gfs2_setxattr,
  931. .getxattr = gfs2_getxattr,
  932. .listxattr = gfs2_listxattr,
  933. .removexattr = gfs2_removexattr,
  934. };