original development tree for Linux kernel GTP module; now long in mainline.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

512 lines
11 KiB

hlist: drop the node parameter from iterators I'm not sure why, but the hlist for each entry iterators were conceived list_for_each_entry(pos, head, member) The hlist ones were greedy and wanted an extra parameter: hlist_for_each_entry(tpos, pos, head, member) Why did they need an extra pos parameter? I'm not quite sure. Not only they don't really need it, it also prevents the iterator from looking exactly like the list iterator, which is unfortunate. Besides the semantic patch, there was some manual work required: - Fix up the actual hlist iterators in linux/list.h - Fix up the declaration of other iterators based on the hlist ones. - A very small amount of places were using the 'node' parameter, this was modified to use 'obj->member' instead. - Coccinelle didn't handle the hlist_for_each_entry_safe iterator properly, so those had to be fixed up manually. The semantic patch which is mostly the work of Peter Senna Tschudin is here: @@ iterator name hlist_for_each_entry, hlist_for_each_entry_continue, hlist_for_each_entry_from, hlist_for_each_entry_rcu, hlist_for_each_entry_rcu_bh, hlist_for_each_entry_continue_rcu_bh, for_each_busy_worker, ax25_uid_for_each, ax25_for_each, inet_bind_bucket_for_each, sctp_for_each_hentry, sk_for_each, sk_for_each_rcu, sk_for_each_from, sk_for_each_safe, sk_for_each_bound, hlist_for_each_entry_safe, hlist_for_each_entry_continue_rcu, nr_neigh_for_each, nr_neigh_for_each_safe, nr_node_for_each, nr_node_for_each_safe, for_each_gfn_indirect_valid_sp, for_each_gfn_sp, for_each_host; type T; expression a,c,d,e; identifier b; statement S; @@ -T b; <+... when != b ( hlist_for_each_entry(a, - b, c, d) S | hlist_for_each_entry_continue(a, - b, c) S | hlist_for_each_entry_from(a, - b, c) S | hlist_for_each_entry_rcu(a, - b, c, d) S | hlist_for_each_entry_rcu_bh(a, - b, c, d) S | hlist_for_each_entry_continue_rcu_bh(a, - b, c) S | for_each_busy_worker(a, c, - b, d) S | ax25_uid_for_each(a, - b, c) S | ax25_for_each(a, - b, c) S | inet_bind_bucket_for_each(a, - b, c) S | sctp_for_each_hentry(a, - b, c) S | sk_for_each(a, - b, c) S | sk_for_each_rcu(a, - b, c) S | sk_for_each_from -(a, b) +(a) S + sk_for_each_from(a) S | sk_for_each_safe(a, - b, c, d) S | sk_for_each_bound(a, - b, c) S | hlist_for_each_entry_safe(a, - b, c, d, e) S | hlist_for_each_entry_continue_rcu(a, - b, c) S | nr_neigh_for_each(a, - b, c) S | nr_neigh_for_each_safe(a, - b, c, d) S | nr_node_for_each(a, - b, c) S | nr_node_for_each_safe(a, - b, c, d) S | - for_each_gfn_sp(a, c, d, b) S + for_each_gfn_sp(a, c, d) S | - for_each_gfn_indirect_valid_sp(a, c, d, b) S + for_each_gfn_indirect_valid_sp(a, c, d) S | for_each_host(a, - b, c) S | for_each_host_safe(a, - b, c, d) S | for_each_mesh_entry(a, - b, c, d) S ) ...+> [akpm@linux-foundation.org: drop bogus change from net/ipv4/raw.c] [akpm@linux-foundation.org: drop bogus hunk from net/ipv6/raw.c] [akpm@linux-foundation.org: checkpatch fixes] [akpm@linux-foundation.org: fix warnings] [akpm@linux-foudnation.org: redo intrusive kvm changes] Tested-by: Peter Senna Tschudin <peter.senna@gmail.com> Acked-by: Paul E. McKenney <paulmck@linux.vnet.ibm.com> Signed-off-by: Sasha Levin <sasha.levin@oracle.com> Cc: Wu Fengguang <fengguang.wu@intel.com> Cc: Marcelo Tosatti <mtosatti@redhat.com> Cc: Gleb Natapov <gleb@redhat.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
9 years ago
  1. /*
  2. * linux/fs/affs/amigaffs.c
  3. *
  4. * (c) 1996 Hans-Joachim Widmaier - Rewritten
  5. *
  6. * (C) 1993 Ray Burr - Amiga FFS filesystem.
  7. *
  8. * Please send bug reports to: hjw@zvw.de
  9. */
  10. #include "affs.h"
  11. extern struct timezone sys_tz;
  12. static char ErrorBuffer[256];
  13. /*
  14. * Functions for accessing Amiga-FFS structures.
  15. */
  16. /* Insert a header block bh into the directory dir
  17. * caller must hold AFFS_DIR->i_hash_lock!
  18. */
  19. int
  20. affs_insert_hash(struct inode *dir, struct buffer_head *bh)
  21. {
  22. struct super_block *sb = dir->i_sb;
  23. struct buffer_head *dir_bh;
  24. u32 ino, hash_ino;
  25. int offset;
  26. ino = bh->b_blocknr;
  27. offset = affs_hash_name(sb, AFFS_TAIL(sb, bh)->name + 1, AFFS_TAIL(sb, bh)->name[0]);
  28. pr_debug("AFFS: insert_hash(dir=%u, ino=%d)\n", (u32)dir->i_ino, ino);
  29. dir_bh = affs_bread(sb, dir->i_ino);
  30. if (!dir_bh)
  31. return -EIO;
  32. hash_ino = be32_to_cpu(AFFS_HEAD(dir_bh)->table[offset]);
  33. while (hash_ino) {
  34. affs_brelse(dir_bh);
  35. dir_bh = affs_bread(sb, hash_ino);
  36. if (!dir_bh)
  37. return -EIO;
  38. hash_ino = be32_to_cpu(AFFS_TAIL(sb, dir_bh)->hash_chain);
  39. }
  40. AFFS_TAIL(sb, bh)->parent = cpu_to_be32(dir->i_ino);
  41. AFFS_TAIL(sb, bh)->hash_chain = 0;
  42. affs_fix_checksum(sb, bh);
  43. if (dir->i_ino == dir_bh->b_blocknr)
  44. AFFS_HEAD(dir_bh)->table[offset] = cpu_to_be32(ino);
  45. else
  46. AFFS_TAIL(sb, dir_bh)->hash_chain = cpu_to_be32(ino);
  47. affs_adjust_checksum(dir_bh, ino);
  48. mark_buffer_dirty_inode(dir_bh, dir);
  49. affs_brelse(dir_bh);
  50. dir->i_mtime = dir->i_ctime = CURRENT_TIME_SEC;
  51. dir->i_version++;
  52. mark_inode_dirty(dir);
  53. return 0;
  54. }
  55. /* Remove a header block from its directory.
  56. * caller must hold AFFS_DIR->i_hash_lock!
  57. */
  58. int
  59. affs_remove_hash(struct inode *dir, struct buffer_head *rem_bh)
  60. {
  61. struct super_block *sb;
  62. struct buffer_head *bh;
  63. u32 rem_ino, hash_ino;
  64. __be32 ino;
  65. int offset, retval;
  66. sb = dir->i_sb;
  67. rem_ino = rem_bh->b_blocknr;
  68. offset = affs_hash_name(sb, AFFS_TAIL(sb, rem_bh)->name+1, AFFS_TAIL(sb, rem_bh)->name[0]);
  69. pr_debug("AFFS: remove_hash(dir=%d, ino=%d, hashval=%d)\n", (u32)dir->i_ino, rem_ino, offset);
  70. bh = affs_bread(sb, dir->i_ino);
  71. if (!bh)
  72. return -EIO;
  73. retval = -ENOENT;
  74. hash_ino = be32_to_cpu(AFFS_HEAD(bh)->table[offset]);
  75. while (hash_ino) {
  76. if (hash_ino == rem_ino) {
  77. ino = AFFS_TAIL(sb, rem_bh)->hash_chain;
  78. if (dir->i_ino == bh->b_blocknr)
  79. AFFS_HEAD(bh)->table[offset] = ino;
  80. else
  81. AFFS_TAIL(sb, bh)->hash_chain = ino;
  82. affs_adjust_checksum(bh, be32_to_cpu(ino) - hash_ino);
  83. mark_buffer_dirty_inode(bh, dir);
  84. AFFS_TAIL(sb, rem_bh)->parent = 0;
  85. retval = 0;
  86. break;
  87. }
  88. affs_brelse(bh);
  89. bh = affs_bread(sb, hash_ino);
  90. if (!bh)
  91. return -EIO;
  92. hash_ino = be32_to_cpu(AFFS_TAIL(sb, bh)->hash_chain);
  93. }
  94. affs_brelse(bh);
  95. dir->i_mtime = dir->i_ctime = CURRENT_TIME_SEC;
  96. dir->i_version++;
  97. mark_inode_dirty(dir);
  98. return retval;
  99. }
  100. static void
  101. affs_fix_dcache(struct inode *inode, u32 entry_ino)
  102. {
  103. struct dentry *dentry;
  104. spin_lock(&inode->i_lock);
  105. hlist_for_each_entry(dentry, &inode->i_dentry, d_alias) {
  106. if (entry_ino == (u32)(long)dentry->d_fsdata) {
  107. dentry->d_fsdata = (void *)inode->i_ino;
  108. break;
  109. }
  110. }
  111. spin_unlock(&inode->i_lock);
  112. }
  113. /* Remove header from link chain */
  114. static int
  115. affs_remove_link(struct dentry *dentry)
  116. {
  117. struct inode *dir, *inode = dentry->d_inode;
  118. struct super_block *sb = inode->i_sb;
  119. struct buffer_head *bh = NULL, *link_bh = NULL;
  120. u32 link_ino, ino;
  121. int retval;
  122. pr_debug("AFFS: remove_link(key=%ld)\n", inode->i_ino);
  123. retval = -EIO;
  124. bh = affs_bread(sb, inode->i_ino);
  125. if (!bh)
  126. goto done;
  127. link_ino = (u32)(long)dentry->d_fsdata;
  128. if (inode->i_ino == link_ino) {
  129. /* we can't remove the head of the link, as its blocknr is still used as ino,
  130. * so we remove the block of the first link instead.
  131. */
  132. link_ino = be32_to_cpu(AFFS_TAIL(sb, bh)->link_chain);
  133. link_bh = affs_bread(sb, link_ino);
  134. if (!link_bh)
  135. goto done;
  136. dir = affs_iget(sb, be32_to_cpu(AFFS_TAIL(sb, link_bh)->parent));
  137. if (IS_ERR(dir)) {
  138. retval = PTR_ERR(dir);
  139. goto done;
  140. }
  141. affs_lock_dir(dir);
  142. /*
  143. * if there's a dentry for that block, make it
  144. * refer to inode itself.
  145. */
  146. affs_fix_dcache(inode, link_ino);
  147. retval = affs_remove_hash(dir, link_bh);
  148. if (retval) {
  149. affs_unlock_dir(dir);
  150. goto done;
  151. }
  152. mark_buffer_dirty_inode(link_bh, inode);
  153. memcpy(AFFS_TAIL(sb, bh)->name, AFFS_TAIL(sb, link_bh)->name, 32);
  154. retval = affs_insert_hash(dir, bh);
  155. if (retval) {
  156. affs_unlock_dir(dir);
  157. goto done;
  158. }
  159. mark_buffer_dirty_inode(bh, inode);
  160. affs_unlock_dir(dir);
  161. iput(dir);
  162. } else {
  163. link_bh = affs_bread(sb, link_ino);
  164. if (!link_bh)
  165. goto done;
  166. }
  167. while ((ino = be32_to_cpu(AFFS_TAIL(sb, bh)->link_chain)) != 0) {
  168. if (ino == link_ino) {
  169. __be32 ino2 = AFFS_TAIL(sb, link_bh)->link_chain;
  170. AFFS_TAIL(sb, bh)->link_chain = ino2;
  171. affs_adjust_checksum(bh, be32_to_cpu(ino2) - link_ino);
  172. mark_buffer_dirty_inode(bh, inode);
  173. retval = 0;
  174. /* Fix the link count, if bh is a normal header block without links */
  175. switch (be32_to_cpu(AFFS_TAIL(sb, bh)->stype)) {
  176. case ST_LINKDIR:
  177. case ST_LINKFILE:
  178. break;
  179. default:
  180. if (!AFFS_TAIL(sb, bh)->link_chain)
  181. set_nlink(inode, 1);
  182. }
  183. affs_free_block(sb, link_ino);
  184. goto done;
  185. }
  186. affs_brelse(bh);
  187. bh = affs_bread(sb, ino);
  188. if (!bh)
  189. goto done;
  190. }
  191. retval = -ENOENT;
  192. done:
  193. affs_brelse(link_bh);
  194. affs_brelse(bh);
  195. return retval;
  196. }
  197. static int
  198. affs_empty_dir(struct inode *inode)
  199. {
  200. struct super_block *sb = inode->i_sb;
  201. struct buffer_head *bh;
  202. int retval, size;
  203. retval = -EIO;
  204. bh = affs_bread(sb, inode->i_ino);
  205. if (!bh)
  206. goto done;
  207. retval = -ENOTEMPTY;
  208. for (size = AFFS_SB(sb)->s_hashsize - 1; size >= 0; size--)
  209. if (AFFS_HEAD(bh)->table[size])
  210. goto not_empty;
  211. retval = 0;
  212. not_empty:
  213. affs_brelse(bh);
  214. done:
  215. return retval;
  216. }
  217. /* Remove a filesystem object. If the object to be removed has
  218. * links to it, one of the links must be changed to inherit
  219. * the file or directory. As above, any inode will do.
  220. * The buffer will not be freed. If the header is a link, the
  221. * block will be marked as free.
  222. * This function returns a negative error number in case of
  223. * an error, else 0 if the inode is to be deleted or 1 if not.
  224. */
  225. int
  226. affs_remove_header(struct dentry *dentry)
  227. {
  228. struct super_block *sb;
  229. struct inode *inode, *dir;
  230. struct buffer_head *bh = NULL;
  231. int retval;
  232. dir = dentry->d_parent->d_inode;
  233. sb = dir->i_sb;
  234. retval = -ENOENT;
  235. inode = dentry->d_inode;
  236. if (!inode)
  237. goto done;
  238. pr_debug("AFFS: remove_header(key=%ld)\n", inode->i_ino);
  239. retval = -EIO;
  240. bh = affs_bread(sb, (u32)(long)dentry->d_fsdata);
  241. if (!bh)
  242. goto done;
  243. affs_lock_link(inode);
  244. affs_lock_dir(dir);
  245. switch (be32_to_cpu(AFFS_TAIL(sb, bh)->stype)) {
  246. case ST_USERDIR:
  247. /* if we ever want to support links to dirs
  248. * i_hash_lock of the inode must only be
  249. * taken after some checks
  250. */
  251. affs_lock_dir(inode);
  252. retval = affs_empty_dir(inode);
  253. affs_unlock_dir(inode);
  254. if (retval)
  255. goto done_unlock;
  256. break;
  257. default:
  258. break;
  259. }
  260. retval = affs_remove_hash(dir, bh);
  261. if (retval)
  262. goto done_unlock;
  263. mark_buffer_dirty_inode(bh, inode);
  264. affs_unlock_dir(dir);
  265. if (inode->i_nlink > 1)
  266. retval = affs_remove_link(dentry);
  267. else
  268. clear_nlink(inode);
  269. affs_unlock_link(inode);
  270. inode->i_ctime = CURRENT_TIME_SEC;
  271. mark_inode_dirty(inode);
  272. done:
  273. affs_brelse(bh);
  274. return retval;
  275. done_unlock:
  276. affs_unlock_dir(dir);
  277. affs_unlock_link(inode);
  278. goto done;
  279. }
  280. /* Checksum a block, do various consistency checks and optionally return
  281. the blocks type number. DATA points to the block. If their pointers
  282. are non-null, *PTYPE and *STYPE are set to the primary and secondary
  283. block types respectively, *HASHSIZE is set to the size of the hashtable
  284. (which lets us calculate the block size).
  285. Returns non-zero if the block is not consistent. */
  286. u32
  287. affs_checksum_block(struct super_block *sb, struct buffer_head *bh)
  288. {
  289. __be32 *ptr = (__be32 *)bh->b_data;
  290. u32 sum;
  291. int bsize;
  292. sum = 0;
  293. for (bsize = sb->s_blocksize / sizeof(__be32); bsize > 0; bsize--)
  294. sum += be32_to_cpu(*ptr++);
  295. return sum;
  296. }
  297. /*
  298. * Calculate the checksum of a disk block and store it
  299. * at the indicated position.
  300. */
  301. void
  302. affs_fix_checksum(struct super_block *sb, struct buffer_head *bh)
  303. {
  304. int cnt = sb->s_blocksize / sizeof(__be32);
  305. __be32 *ptr = (__be32 *)bh->b_data;
  306. u32 checksum;
  307. __be32 *checksumptr;
  308. checksumptr = ptr + 5;
  309. *checksumptr = 0;
  310. for (checksum = 0; cnt > 0; ptr++, cnt--)
  311. checksum += be32_to_cpu(*ptr);
  312. *checksumptr = cpu_to_be32(-checksum);
  313. }
  314. void
  315. secs_to_datestamp(time_t secs, struct affs_date *ds)
  316. {
  317. u32 days;
  318. u32 minute;
  319. secs -= sys_tz.tz_minuteswest * 60 + ((8 * 365 + 2) * 24 * 60 * 60);
  320. if (secs < 0)
  321. secs = 0;
  322. days = secs / 86400;
  323. secs -= days * 86400;
  324. minute = secs / 60;
  325. secs -= minute * 60;
  326. ds->days = cpu_to_be32(days);
  327. ds->mins = cpu_to_be32(minute);
  328. ds->ticks = cpu_to_be32(secs * 50);
  329. }
  330. umode_t
  331. prot_to_mode(u32 prot)
  332. {
  333. umode_t mode = 0;
  334. if (!(prot & FIBF_NOWRITE))
  335. mode |= S_IWUSR;
  336. if (!(prot & FIBF_NOREAD))
  337. mode |= S_IRUSR;
  338. if (!(prot & FIBF_NOEXECUTE))
  339. mode |= S_IXUSR;
  340. if (prot & FIBF_GRP_WRITE)
  341. mode |= S_IWGRP;
  342. if (prot & FIBF_GRP_READ)
  343. mode |= S_IRGRP;
  344. if (prot & FIBF_GRP_EXECUTE)
  345. mode |= S_IXGRP;
  346. if (prot & FIBF_OTR_WRITE)
  347. mode |= S_IWOTH;
  348. if (prot & FIBF_OTR_READ)
  349. mode |= S_IROTH;
  350. if (prot & FIBF_OTR_EXECUTE)
  351. mode |= S_IXOTH;
  352. return mode;
  353. }
  354. void
  355. mode_to_prot(struct inode *inode)
  356. {
  357. u32 prot = AFFS_I(inode)->i_protect;
  358. umode_t mode = inode->i_mode;
  359. if (!(mode & S_IXUSR))
  360. prot |= FIBF_NOEXECUTE;
  361. if (!(mode & S_IRUSR))
  362. prot |= FIBF_NOREAD;
  363. if (!(mode & S_IWUSR))
  364. prot |= FIBF_NOWRITE;
  365. if (mode & S_IXGRP)
  366. prot |= FIBF_GRP_EXECUTE;
  367. if (mode & S_IRGRP)
  368. prot |= FIBF_GRP_READ;
  369. if (mode & S_IWGRP)
  370. prot |= FIBF_GRP_WRITE;
  371. if (mode & S_IXOTH)
  372. prot |= FIBF_OTR_EXECUTE;
  373. if (mode & S_IROTH)
  374. prot |= FIBF_OTR_READ;
  375. if (mode & S_IWOTH)
  376. prot |= FIBF_OTR_WRITE;
  377. AFFS_I(inode)->i_protect = prot;
  378. }
  379. void
  380. affs_error(struct super_block *sb, const char *function, const char *fmt, ...)
  381. {
  382. va_list args;
  383. va_start(args,fmt);
  384. vsnprintf(ErrorBuffer,sizeof(ErrorBuffer),fmt,args);
  385. va_end(args);
  386. printk(KERN_CRIT "AFFS error (device %s): %s(): %s\n", sb->s_id,
  387. function,ErrorBuffer);
  388. if (!(sb->s_flags & MS_RDONLY))
  389. printk(KERN_WARNING "AFFS: Remounting filesystem read-only\n");
  390. sb->s_flags |= MS_RDONLY;
  391. }
  392. void
  393. affs_warning(struct super_block *sb, const char *function, const char *fmt, ...)
  394. {
  395. va_list args;
  396. va_start(args,fmt);
  397. vsnprintf(ErrorBuffer,sizeof(ErrorBuffer),fmt,args);
  398. va_end(args);
  399. printk(KERN_WARNING "AFFS warning (device %s): %s(): %s\n", sb->s_id,
  400. function,ErrorBuffer);
  401. }
  402. /* Check if the name is valid for a affs object. */
  403. int
  404. affs_check_name(const unsigned char *name, int len)
  405. {
  406. int i;
  407. if (len > 30)
  408. #ifdef AFFS_NO_TRUNCATE
  409. return -ENAMETOOLONG;
  410. #else
  411. len = 30;
  412. #endif
  413. for (i = 0; i < len; i++) {
  414. if (name[i] < ' ' || name[i] == ':'
  415. || (name[i] > 0x7e && name[i] < 0xa0))
  416. return -EINVAL;
  417. }
  418. return 0;
  419. }
  420. /* This function copies name to bstr, with at most 30
  421. * characters length. The bstr will be prepended by
  422. * a length byte.
  423. * NOTE: The name will must be already checked by
  424. * affs_check_name()!
  425. */
  426. int
  427. affs_copy_name(unsigned char *bstr, struct dentry *dentry)
  428. {
  429. int len = min(dentry->d_name.len, 30u);
  430. *bstr++ = len;
  431. memcpy(bstr, dentry->d_name.name, len);
  432. return len;
  433. }