ops_inode.c 26 KB

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